/** * @Description 简单题开始,这傻逼题用了半小时来做 * 我是真的菜啊,愁死我,我好像是个憨批 * @Date 2022/1/29 2:49 下午 * @Author wuqichuan@zuoyebang.com **/public class Solution { public boolean isValid(String s) { HashMap<Character,Character> map = new HashMap<>(); map.put(')','('); map.put(']','['); map.put('}','{'); Stack<Character> stack = new Stack<>(); char[] chars = s.toCharArray(); for(Character c : chars){ if(map.containsKey(c)){ if(stack.isEmpty() || stack.peek() != map.get(c)){ return false; }else { stack.pop(); } }else { stack.push(c); } } return stack.isEmpty(); }}