-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0020. Valid Parentheses.cpp
37 lines (31 loc) · 1.06 KB
/
0020. Valid Parentheses.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Date: Feb. 13, 2021
// Runtime: 0 ms, faster than 100.00% of C++ online submissions for Valid Parentheses.
// Memory Usage: 6.3 MB, less than 74.31% of C++ online submissions for Valid Parentheses.
class Solution {
public:
bool isValid(string s) {
stack<char> t;
for (auto it = s.begin(); it != s.end(); it++) {
if (*it == '(' || *it == '{' || *it == '[') {
t.push(*it);
continue;
}
else if (*it == ')') {
if (t.empty()) return false;
if (t.top() != '(') return false;
else t.pop();
}
else if (*it == '}') {
if (t.empty()) return false;
if (t.top() != '{') return false;
else t.pop();
}
else if (*it == ']') {
if (t.empty()) return false;
if (t.top() != '[') return false;
else t.pop();
}
}
return t.empty();
}
};