唯一區(qū)別就是輸入的bracket不局限于那三種 "{" "[" "("
所以給了兩個(gè)輔助方法,一個(gè)是iSOPen(char)用來(lái)判斷是不是前括號(hào)忘嫉,另一個(gè)是 findClosed(char)用來(lái)找出前括號(hào)所對(duì)應(yīng)的后括號(hào)
public class Solution {
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();
}
}