leetcode:有效括号java

class Solution {
    public boolean isValid(String s) {
        HashMap<Character,Character> juge = new HashMap();
        juge.put(')', '(');
        juge.put('}', '{');
        juge.put(']', '[');
        Stack<Character> res = new Stack();
        for (int i = 0; i < s.length(); i++) {
            char k = s.charAt(i);
            if (juge.containsKey(k)) {
                if (res.isEmpty()) {
                    return false;
                } else {
                    char m = res.pop();
                    if (m != juge.get(k)) {
                        return false;
                    }
                }
            } else {
                res.push(s.charAt(i));
            }


        }

        return res.empty();
    }
}

猜你喜欢

转载自blog.csdn.net/microopithecus/article/details/83987308