Skip to content

Latest commit

 

History

History
26 lines (25 loc) · 778 Bytes

020.md

File metadata and controls

26 lines (25 loc) · 778 Bytes
class Solution {
    String left = "({[";
    public boolean isValid(String s) {
        Stack mStack = new Stack<>();
        HashMap<String, String> hashMap = new HashMap(){};
        hashMap.put(")", "(");
        hashMap.put("]", "[");
        hashMap.put("}", "{");
        for (int i = 0; i < s.length(); i++) {
            String one = s.substring(i, i + 1);
            if (hashMap.containsValue(one)){
                //左括号
                mStack.push(one);
            }else {
             
                if (mStack.isEmpty()||!((String) mStack.pop()).equals(hashMap.get(one))) {
                    return false;
                }
            }
        }
        return mStack.isEmpty();
    }
}

https://leetcode-cn.com/problems/valid-parentheses/