public boolean isValid(String s) { Stack<Character> stack = new Stack<>(); Map<Character,Character> map = new HashMap<>(); char[] chars = s.toCharArray(); map.put(')','('); map.put('}','{'); map.put(']','['); for(int i=0;i < s.length();i++){ if(!map.containsKey(chars[i])) { //为左括号时直接入栈 stack.push(chars[i]); }else{ //为右括号时,如果栈为空或者栈顶与该括号类型不匹配返回false if(stack.empty() || map.get(chars[i]) != stack.pop()){ return false; } } } //字符串遍历完毕后,如果栈为空返回true,反之返回false return stack.empty(); }
标签:map,false,chars,括号,put,符合规范,jjhd,stack From: https://www.cnblogs.com/yingpu/p/17796331.html