您的位置:首页 > 其它

20 Valid Parentheses

2015-08-20 11:10 232 查看
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;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: