-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution20.cpp
More file actions
56 lines (53 loc) · 1.31 KB
/
Solution20.cpp
File metadata and controls
56 lines (53 loc) · 1.31 KB
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
47
48
49
50
51
52
53
54
55
56
//
// Solution20.cpp
// Algorithm
//
// Created by Pancf on 2020/8/8.
// Copyright © 2020 Pancf. All rights reserved.
//
#include "Solution20.hpp"
#include <stack>
using std::stack;
bool Solution20::isValid(string s)
{
if (s.size() == 0) return true;
if (s.size() == 1) return false;
std::stack<char> st;
st.push(s[0]);
bool shouldContinue = true;
for (int i = 1; i < s.size(); ++i) {
char c = s[i];
switch (c) {
case '(':
case '[':
case '{':
st.push(c);
break;
case ')':
if (!st.empty() && '(' == st.top()) {
st.pop();
} else {
shouldContinue = false;
}
break;
case ']':
if (!st.empty() && '[' == st.top()) {
st.pop();
} else {
shouldContinue = false;
}
break;
case '}':
if (!st.empty() && '{' == st.top()) {
st.pop();
} else {
shouldContinue = false;
}
break;
}
if (!shouldContinue) {
break;
}
}
return st.empty() && shouldContinue;
}