LeetCode20有效的括号
public class Solution {
public boolean isValid(String s){
int n = s.length();
if (n % 2 == 1) {
return false;
}
HashMap<Character, Character> map = new HashMap<>();
map.put(')','(');
map.put(']','[');
map.put('}','{');
Deque<Character> stack = new LinkedList<>();
for (int i = 0; i < n; i++) {
char at = s.charAt(i);
if (map.containsKey(at)){
if (stack.isEmpty() || stack.peek() != map.get(at)){
return false;
}
stack.pop();
} else {
stack.push(at);
}
}
return stack.isEmpty();
}
}