https://leetcode-cn.com/problems/valid-parentheses/
public boolean isValid4(String s) {
if (s.length()==0 || s.length()==1) {
return false;
}
HashMap<Character, Character> newmap = new HashMap<>();
newmap.put('(', ')');
newmap.put('[', ']');
newmap.put('{', '}');
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(' || c == '[' || c == '{' ) {
stack.push(c);
}else {
if (stack.isEmpty()) return false;
char left = stack.pop();
if (c != newmap.get(left)) {
return false;
}
}
}
return stack.isEmpty();
}
20年b站的面试题,第二遍做