Re: [LeetCode] 刷到面試 Grind169 C++
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;
}
};
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.144.184 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1678246265.A.B96.html
推
03/08 11:49,
2年前
, 1F
03/08 11:49, 1F
→
03/08 12:13,
2年前
, 2F
03/08 12:13, 2F
※ 編輯: SuiseiLeda (140.113.144.184 臺灣), 03/08/2023 12:14:08
討論串 (同標題文章)
完整討論串 (本文為第 2 之 14 篇):