码迷,mamicode.com
首页 > 编程语言 > 详细

对如下字符串(234453)[234]{2324}分析它的括号使用是否正确,括号匹配(Java实现)

时间:2017-10-18 14:09:40      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:main   括号匹配   turn   out   print   实现   []   匹配   else   

我的一种思路是递归:

private static String s = "(2344[)]53[234]{2324}";
private static boolean f(int x, int y) {
    for (int i = x; i < s.length(); i++) {
        if (s.charAt(i) == ‘(‘) {
            return f(i + 1, 1);
        } else if (s.charAt(i) == ‘[‘) {
            return f(i + 1, 2);
        } else if (s.charAt(i) == ‘{‘) {
            return f(i + 1, 3);
        } else if (s.charAt(i) == ‘)‘) {
            return y == 1;
        } else if (s.charAt(i) == ‘]‘) {
            return y == 2;
        } else if (s.charAt(i) == ‘}‘) {
            return y == 3;
        }
    }
    return true;
}
public static void main(String[] args) {
       System.out.println(f(0,0));
}

 

对如下字符串(234453)[234]{2324}分析它的括号使用是否正确,括号匹配(Java实现)

标签:main   括号匹配   turn   out   print   实现   []   匹配   else   

原文地址:http://www.cnblogs.com/acm-bingzi/p/baidu1.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!