-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValid_Parentheses.cpp
37 lines (35 loc) · 1.05 KB
/
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
// https://leetcode.com/problems/valid-parentheses/
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(int i = 0; i < s.size(); i++) {
if(s[i] == '(' || s[i] == '{' || s[i] == '['){
st.push(s[i]);
}
if(s[i] == ')') {
if(st.empty() || st.top() != '(')
return false;
st.pop();
}
if(s[i] == '}') {
if(st.empty() || st.top() != '{')
return false;
st.pop();
}
if(s[i] == ']') {
if(st.empty() || st.top() != '[')
return false;
st.pop();
}
}
if(st.empty() == 0)
return false;
return true;
}
};
/*
* notes:
* 注意 stack 的pop 函数返回的是 void 类型,只是删除了最后一个元素,
* 但不返回这个元素,这是容易犯错的(compile error).
*/