public static boolean parentheses(String s){
LinkedStack<Character> stack = new LinkedStack<>();
for(int i = 0; i < s.length(); i++){
switch(s.charAt(i)){
case '(': stack.push('('); break;
case '[': stack.push('['); break;
case '{': stack.push('{'); break;
case ')': if(stack.pop() != '(') return false; break;
case ']': if(stack.pop() != '[') return false; break;
case '}': if(stack.pop() != '{') return false; break;
}
}
return stack.isEmpty();
}
网友评论