lurenaa的博客

🥛stack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
bool isValid(string s) {
stack<char> stk;
for(auto c : s) {
if(!stk.size()) {
stk.push(c);
continue;
}
int t = stk.top();
if(t == '{' && c == '}' ||
t == '(' && c == ')' ||
t == '[' && c == ']')
{
stk.pop();
continue;
}
stk.push(c);
}
return stk.size() == 0;
}
};

Accepted

76/76 cases passed (4 ms)

Your runtime beats 69.91 % of cpp submissions

Your memory usage beats 5.33 % of cpp submissions (8.7 MB)