-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20.h
46 lines (41 loc) · 1.1 KB
/
20.h
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
38
39
40
41
42
43
44
45
46
//
// Created by 17336 on 2021/11/8.
//
#ifndef HOT100_20_H
#define HOT100_20_H
#endif //HOT100_20_H
#include "stack"
using namespace std;
class Solution {
public:
bool isValid(string s) {
stack<int> stack;
for (const auto &item: s) {
switch (item) {
case '{':
stack.push(item);
break;
case '[':
stack.push(item);
break;
case '(':
stack.push(item);
break;
case ')':
if(stack.empty()||stack.top()!='(') return false;
stack.pop();
break;
case ']':
if(stack.empty()||stack.top()!='[') return false;
stack.pop();
break;
case '}':
if(stack.empty()||stack.top()!='{') return false;
stack.pop();
break;
}
}
if(stack.empty()) return true;
return false;
}
};