Re: [LeetCode] 刷到面试 Grind169 C++

楼主: SuiseiLeda (星街雷达)   2023-03-08 11:31:03
Valid Parentheses easy题

写了这题我才发现我不会用stack
以前作业全都用vector+双循环写
我好可悲
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(int i =0; i<s.length(); i++){
char c = s[i];
if(c == '(' or c == '[' or c == '{'){
st.push(c);
}else{
if(st.empty()){
return false;
}
if(c == ')' and st.top() == '(' or
c == ']' and st.top() == '[' or
c == '}' and st.top() == '{' ){
st.pop();
}else{
return false;
}
}
}
if(st.empty()){
return true;
}
return false;
}
};
作者: idiont (supertroller)   2023-03-08 11:49:00
为啥你的题目名称跟你写的东西不一样
楼主: SuiseiLeda (星街雷达)   2023-03-08 12:13:00
靠邀 那是下一题 我耍白痴

Links booklink

Contact Us: admin [ a t ] ucptt.com