Leetcode-cn.com/problems/va…
class Solution {
public static boolean isValid(String s) {
if (s.length() == 0) {
return true;
}
HashMap<Character, Character> map = new HashMap<>();
map.put('('.') ');
map.put('['.'] ');
map.put('{'.'} '); Stack<Character> Stack = new Stack<>();for (int i = 0; i < s.length(); i++) {
if (map.containsKey(s.charAt(i))) {
stack.push(s.charAt(i));
} else {
if(stack.isEmpty() || map.get(stack.pop()) ! = s.charAt(i)) {return false; }}}returnstack.isEmpty(); }}Copy the code