Leetcode - 20 Valid Parenthese (Easy)
题目描述:给定字符串,判断括号是否有效。
Input: "()[]{}"
Output: true
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (Character c : s.toCharArray()){
if(c == '('){
stack.push(')');
}else if(c == '['){
stack.push(']');
}else if(c == '{'){
stack.push('}');
}else if(stack.isEmpty() || stack.pop() != c){
return false;
}
}
return stack.isEmpty();
}