LeetCode第20题

LeetCode20题不多说上代码

public boolean isValid(String s){
Stack<Character> stack = new Stack<Character>();
for (int i=0;i<s.length();i++){
char c = s.charAt(i);
if (c=='('||c=='['||c=='{')
stack.push(c);
else{
//栈没字符匹配失败
if (stack.isEmpty())
return false;
char topChar = stack.pop();
if (c==')'&&topChar!='(')
return false;
if (c==']'&&topChar!='[')
return false;
if (c=='}'&&topChar!='{')
return false;

}}
return stack.isEmpty();
}

 

posted on 2018-08-12 21:31  JETIME庚  阅读(382)  评论(0编辑  收藏  举报

导航