-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20.cpp
More file actions
38 lines (35 loc) · 923 Bytes
/
20.cpp
File metadata and controls
38 lines (35 loc) · 923 Bytes
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
//
// 20.cpp
// LeetCode
//
// Created by 张佐玮 on 15/6/29.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Valid Parentheses
//
#include <iostream>
#include <stack>
using namespace std;
class Solution {
public:
bool isValid(string s) {
stack<char> leftParentheses;
for (auto ch : s) {
if (ch == '(' || ch == '[' || ch == '{') {
leftParentheses.push(ch);
}
else if (leftParentheses.empty()) {
return false;
}
else if ((ch == ')' && leftParentheses.top() == '(') ||
(ch == ']' && leftParentheses.top() == '[') ||
(ch == '}' && leftParentheses.top() == '{')) {
leftParentheses.pop();
}
else {
return false;
}
}
return leftParentheses.empty();
}
};