Skip to content

Commit d8fdeb6

Browse files
authored
Merge pull request doocs#34 from KongJHong/master
Add Solution 020 [CPP]
2 parents a68cf97 + 5b562ea commit d8fdeb6

File tree

2 files changed

+165
-1
lines changed

2 files changed

+165
-1
lines changed

solution/020.Valid Parentheses/README.md

+86-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
### 解法
4343
遍历 string,遇到左括号,压入栈中;遇到右括号,从栈中弹出元素,元素不存在或者元素与该右括号不匹配,返回 false。遍历结束,栈为空则返回 true,否则返回 false。
4444

45-
```java
45+
因为字符串只包含"(){}[]",也可以进行特殊处理,用映射来做
46+
47+
#### java
48+
```
4649
class Solution {
4750
public boolean isValid(String s) {
4851
if (s == null || s == "") {
@@ -80,4 +83,86 @@ class Solution {
8083
return a == ')' || a == ']' || a == '}';
8184
}
8285
}
86+
```
87+
88+
#### C++
89+
```
90+
91+
class Solution {
92+
public:
93+
bool isValid(string s) {
94+
stack<char> _stack;
95+
int len = s.length();
96+
if(len == 0)return true;
97+
char ch;
98+
for(int i= 0;i<len;i++)
99+
{
100+
if(s[i] == '{' ||s[i] == '['||s[i] == '(' )
101+
{
102+
_stack.push(s[i]);
103+
}
104+
if(s[i] == '}')
105+
{
106+
if(_stack.empty())return false;
107+
else ch = _stack.top();
108+
109+
if(ch != '{')return false;
110+
else _stack.pop();
111+
112+
}
113+
else if(s[i] == ']')
114+
{
115+
if(_stack.empty())return false;
116+
else ch = _stack.top();
117+
118+
if(ch != '[')return false;
119+
else _stack.pop();
120+
}
121+
else if(s[i] == ')')
122+
{
123+
if(_stack.empty())return false;
124+
else ch = _stack.top();
125+
126+
if(ch != '(')return false;
127+
else _stack.pop();
128+
}
129+
}
130+
131+
if(!_stack.empty())return false;
132+
133+
return true;
134+
135+
}
136+
};
137+
138+
//特殊
139+
class Solution {
140+
public:
141+
bool isValid(string s) {
142+
map<char,int> m={
143+
{'[',1},
144+
{']',-1},
145+
{'{',2},
146+
{'}',-2},
147+
{'(',3},
148+
{')',-3}
149+
};
150+
stack<int> sk;
151+
for(int i=0;i<s.length();i++){
152+
if(m[s[i]]<0 ){
153+
if(!sk.empty() && sk.top()==(-m[s[i]])){
154+
sk.pop();
155+
}else{
156+
return false;
157+
}
158+
}else{
159+
sk.push(m[s[i]]);
160+
}
161+
}
162+
if(sk.empty())
163+
return true;
164+
return false;
165+
}
166+
};
167+
83168
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
class Solution {
2+
public:
3+
bool isValid(string s) {
4+
stack<char> _stack;
5+
int len = s.length();
6+
if(len == 0)return true;
7+
char ch;
8+
for(int i= 0;i<len;i++)
9+
{
10+
if(s[i] == '{' ||s[i] == '['||s[i] == '(' )
11+
{
12+
_stack.push(s[i]);
13+
}
14+
if(s[i] == '}')
15+
{
16+
if(_stack.empty())return false;
17+
else ch = _stack.top();
18+
19+
if(ch != '{')return false;
20+
else _stack.pop();
21+
22+
}
23+
else if(s[i] == ']')
24+
{
25+
if(_stack.empty())return false;
26+
else ch = _stack.top();
27+
28+
if(ch != '[')return false;
29+
else _stack.pop();
30+
}
31+
else if(s[i] == ')')
32+
{
33+
if(_stack.empty())return false;
34+
else ch = _stack.top();
35+
36+
if(ch != '(')return false;
37+
else _stack.pop();
38+
}
39+
}
40+
41+
if(!_stack.empty())return false;
42+
43+
return true;
44+
45+
}
46+
};
47+
48+
49+
50+
-----------------
51+
//特殊
52+
class Solution {
53+
public:
54+
bool isValid(string s) {
55+
map<char,int> m={
56+
{'[',1},
57+
{']',-1},
58+
{'{',2},
59+
{'}',-2},
60+
{'(',3},
61+
{')',-3}
62+
};
63+
stack<int> sk;
64+
for(int i=0;i<s.length();i++){
65+
if(m[s[i]]<0 ){
66+
if(!sk.empty() && sk.top()==(-m[s[i]])){
67+
sk.pop();
68+
}else{
69+
return false;
70+
}
71+
}else{
72+
sk.push(m[s[i]]);
73+
}
74+
}
75+
if(sk.empty())
76+
return true;
77+
return false;
78+
}
79+
};

0 commit comments

Comments
 (0)