题目链接:LeetCode20
分析:括号匹配
思路:栈
我的代码:
没有提早判断,脑子坏掉了。
class Solution {
public boolean isValid(String s) {
Map<Character,Character> map=new HashMap<>(){
{
put('(',')');
put('{','}');
put('[',']');
}
};
Stack<Character> stack=new Stack<>();
for(int i=0;i<s.length();i++){
if(!stack.isEmpty()&&map.containsKey(stack.peek())
&&map.get(stack.peek()).equals(s.charAt(i)))stack.pop();
else stack.push(s.charAt(i));
}
if(stack.isEmpty())return true;
return false;
}
}