Leetcode每日一题_20
public static boolean isValid(String s) {//o(n) o(n)
if(s==null||s.length()==0)return true;
Stack<Character> stack=new Stack<>();
for(Character c:s.toCharArray()) {
if (c=='(') {
stack.push(')');
}
else if (c=='[') {
stack.push(']');
}
else if (c=='{') {
stack.push('}');
}else {
if (stack.isEmpty()||stack.pop()!=c) {
return false;
}
}
}
return stack.isEmpty();
}
public static boolean isValid(String s) {//o(n) o(n)
if(s==null||s.length()==0)return true;
Stack<Character> stack=new Stack<>();
for(Character c:s.toCharArray()) {
if (c=='(') {
stack.push(')');
}
else if (c=='[') {
stack.push(']');
}
else if (c=='{') {
stack.push('}');
}else {
if (stack.isEmpty()||stack.pop()!=c) {
return false;
}
}
}
return stack.isEmpty();
}
全部评论
相关推荐
07-07 12:24
门头沟学院 机械工程师 点赞 评论 收藏
分享
06-23 11:43
门头沟学院 Java 点赞 评论 收藏
分享
06-30 08:52
广东食品药品职业学院 C++ 
点赞 评论 收藏
分享
Java抽象小篮子:go批这招太狠了
点赞 评论 收藏
分享