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();
}
全部评论
相关推荐
点赞 评论 收藏
分享
03-28 16:09
营口理工学院 新材料/环保技术 点赞 评论 收藏
分享
点赞 评论 收藏
分享