Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add Solution20[CPP] #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 1 commit into doocs:master from KongJHong:master
Oct 20, 2018
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
View file Open in desktop
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
View file Open in desktop
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;
}
};

AltStyle によって変換されたページ (->オリジナル) /