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