import java.util.*;
public class Solution {
public boolean isValid(String s) {
if(s==null)
return true;
if(s.length()%2==1)
return false;
Stack<Character> stack=new Stack<Character>();
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='('||s.charAt(i)=='{'||s.charAt(i)=='['){
stack.push(s.charAt(i));
}
else if(!stack.empty()&&s.charAt(i)==')'){
if(stack.pop()!='(')
return false;
}
else if(!stack.empty()&&s.charAt(i)=='}'){
if(stack.pop()!='{')
return false;
}
else if(!stack.empty()&&s.charAt(i)==']'){
if(stack.pop()!='[')
return false;
}
else{
return false;
}
}
if(stack.empty()){
return true;
}
else{
return false;
}
}
}
07-18
62
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交