-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,10 @@ | |
| ### 解法 | ||
| 遍历 string,遇到左括号,压入栈中;遇到右括号,从栈中弹出元素,元素不存在或者元素与该右括号不匹配,返回 false。遍历结束,栈为空则返回 true,否则返回 false。 | ||
|
|
||
| ```java | ||
| 因为字符串只包含"(){}[]",也可以进行特殊处理,用映射来做 | ||
|
|
||
| #### java | ||
| ``` | ||
| class Solution { | ||
| public boolean isValid(String s) { | ||
| if (s == null || s == "") { | ||
|
|
@@ -80,4 +83,86 @@ class Solution { | |
| return a == ')' || a == ']' || a == '}'; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| #### C++ | ||
| ``` | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 写代码块的时候,可以加上特定的语言噢,识别度较高~ ```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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.