package 括号匹配;


import java.util.Stack;

public class 括号匹配 {
    public static void main(String[] args) {
        
        Stack<Character> stack = new Stack<Character>();
        char[] ch = { ')', ')', '[', ']' };
        boolean flag = true;
        for (int i = 0; i < ch.length; i++) {
            if ((i == 0) && (ch[i] == ')' || ch[i] == ']')) {
                System.out.println("no0");
                flag = false;
                break;
            }

            switch (ch[i]) {
            case '(':
                stack.push(ch[i]);
                break;
            case ')':
                if (stack.isEmpty()) {
                    System.out.println("no1");
                    flag = false;
                    break;
                } else {
                    if (!stack.pop().equals('(')) {
                        System.out.println("no2");
                        flag = false;
                        break;
                    }
                }

                break;
            case '[':
                stack.push(ch[i]);
                break;
            case ']':
                if (stack.isEmpty()) {
                    System.out.println("no3");
                    flag = false;
                    break;
                } else {
                    if (!stack.pop().equals('[')) {
                        System.out.println("no4");
                        flag = false;
                        break;
                    }
                }
                break;
            }

        }

        if (flag) {
            System.out.println("ok");
        }
    }
}

 

欢迎关注微信公众号:大数据从业者