原题链接:https://oj.leetcode.com/problems/valid-parentheses/
这题简单。一次过。
public class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
char c;
for(int i = 0; i < s.length(); i++){
c = s.charAt(i);
if(c == '(' || c == '[' || c == '{'){
stack.push(c);
}
else if (c == ')' || c == ']' || c == '}'){
//dont forget this: there are more right parenthese
if(stack.size() == 0) return false;
char d = stack.peek();
if(c==')' && d!='(') return false;
if(c==']' && d!='[') return false;
if(c=='}' && d!='{') return false;
else stack.pop();
}
}
//return true;
return stack.size() == 0;
}
}