标签:push val 联系 hashmap turn string 一个 字符 是否有效
class Solution { public boolean isValid(String s) { int len = s.length(); if(len == 0) return true; Stack<Character> stack = new Stack<>(); Map<Character,Character> map = new HashMap<Character, Character>(){ { put(‘(‘,‘)‘); put(‘[‘,‘]‘); put(‘{‘,‘}‘); } }; for(int i = 0; i < len; i++){ if(map.containsKey(s.charAt(i))){ stack.push(s.charAt(i)); }else if(!stack.empty() && map.get(stack.peek()) == s.charAt(i)){ stack.pop(); }else{ return false; } } return stack.empty(); } }
标签:push val 联系 hashmap turn string 一个 字符 是否有效
原文地址:https://www.cnblogs.com/buaaZhhx/p/12378004.html