Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 86 additions & 1 deletion solution/020.Valid Parentheses/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
### 解法
遍历 string,遇到左括号,压入栈中;遇到右括号,从栈中弹出元素,元素不存在或者元素与该右括号不匹配,返回 false。遍历结束,栈为空则返回 true,否则返回 false。

```java
因为字符串只包含"(){}[]",也可以进行特殊处理,用映射来做

#### java
```
class Solution {
public boolean isValid(String s) {
if (s == null || s == "") {
Expand Down Expand Up @@ -80,4 +83,86 @@ class Solution {
return a == ')' || a == ']' || a == '}';
}
}
```

#### C++
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

写代码块的时候,可以加上特定的语言噢,识别度较高~
e.g.

```cpp

code here...

```
```java

code here...

```


class Solution {
public:
bool isValid(string s) {
stack<char> _stack;
int len = s.length();
if(len == 0)return true;
char ch;
for(int i= 0;i<len;i++)
{
if(s[i] == '{' ||s[i] == '['||s[i] == '(' )
{
_stack.push(s[i]);
}
if(s[i] == '}')
{
if(_stack.empty())return false;
else ch = _stack.top();

if(ch != '{')return false;
else _stack.pop();

}
else if(s[i] == ']')
{
if(_stack.empty())return false;
else ch = _stack.top();

if(ch != '[')return false;
else _stack.pop();
}
else if(s[i] == ')')
{
if(_stack.empty())return false;
else ch = _stack.top();

if(ch != '(')return false;
else _stack.pop();
}
}

if(!_stack.empty())return false;

return true;

}
};

//特殊
class Solution {
public:
bool isValid(string s) {
map<char,int> m={
{'[',1},
{']',-1},
{'{',2},
{'}',-2},
{'(',3},
{')',-3}
};
stack<int> sk;
for(int i=0;i<s.length();i++){
if(m[s[i]]<0 ){
if(!sk.empty() && sk.top()==(-m[s[i]])){
sk.pop();
}else{
return false;
}
}else{
sk.push(m[s[i]]);
}
}
if(sk.empty())
return true;
return false;
}
};

```
79 changes: 79 additions & 0 deletions solution/020.Valid Parentheses/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
class Solution {
public:
bool isValid(string s) {
stack<char> _stack;
int len = s.length();
if(len == 0)return true;
char ch;
for(int i= 0;i<len;i++)
{
if(s[i] == '{' ||s[i] == '['||s[i] == '(' )
{
_stack.push(s[i]);
}
if(s[i] == '}')
{
if(_stack.empty())return false;
else ch = _stack.top();

if(ch != '{')return false;
else _stack.pop();

}
else if(s[i] == ']')
{
if(_stack.empty())return false;
else ch = _stack.top();

if(ch != '[')return false;
else _stack.pop();
}
else if(s[i] == ')')
{
if(_stack.empty())return false;
else ch = _stack.top();

if(ch != '(')return false;
else _stack.pop();
}
}

if(!_stack.empty())return false;

return true;

}
};



-----------------
//特殊
class Solution {
public:
bool isValid(string s) {
map<char,int> m={
{'[',1},
{']',-1},
{'{',2},
{'}',-2},
{'(',3},
{')',-3}
};
stack<int> sk;
for(int i=0;i<s.length();i++){
if(m[s[i]]<0 ){
if(!sk.empty() && sk.top()==(-m[s[i]])){
sk.pop();
}else{
return false;
}
}else{
sk.push(m[s[i]]);
}
}
if(sk.empty())
return true;
return false;
}
};