题解 | #括号序列#
括号序列
http://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
import java.util.*; public class Solution { /** * * @param s string字符串 * @return bool布尔型 */ public boolean isValid (String s) { // write code here int n = s.length(); if (n % 2 == 1) { return false; } Deque<Character> stack = new LinkedList<>(); HashMap<Character,Character> map = new HashMap<Character,Character>(){{ put(']','['); put(')','('); put('}','{'); }}; for(int i = 0 ; i < s.length(); i++){ if(map.containsKey(s.charAt(i))){ if(!stack.isEmpty() && stack.peek() == map.get(s.charAt(i))){ stack.pop(); }else{ return false; } }else{ stack.push(s.charAt(i)); } } return stack.isEmpty(); } }