20Valid Parentheses

来源:互联网 发布:苹果拍照软件搞怪 编辑:程序博客网 时间:2024/06/06 09:21
public class Solution {
     public boolean isValid(String s) {
 if(s==null) return true;
     Stack<Character> stack = new Stack<Character>();
     int len = s.length();
     int i = 0;
     while(i < len){
     char c = s.charAt(i);
     if(c=='('||c=='{'||c=='['){
     stack.push(c);
     }else{
     if(stack.isEmpty()) return false;
     char tmp = stack.peek();
     if((tmp=='(' && c==')') || (tmp=='{' && c=='}') || (tmp=='[' && c==']')){
     stack.pop();
     }else{
     return false;
     }
     }
     ++i;
     }
     if(stack.isEmpty()){
     return true;
     }else{
     return false;
     }
}
}
0 0
原创粉丝点击