-
Notifications
You must be signed in to change notification settings - Fork 1
redandsilver-6 #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
Open
redandsilver
wants to merge
1
commit into
main
Choose a base branch
from
redandsilver
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
redandsilver-6 #34
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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| class Solution { | ||
| public: | ||
| bool isValid(string s) { | ||
| stack<char> st; | ||
| for(char c : s){ | ||
| if(c=='(' || c=='{' || c=='['){ | ||
| st.push(c); | ||
| }else{ | ||
| if (st.empty() || | ||
| (c == ')' && st.top() != '(') || | ||
| (c == '}' && st.top() != '{') || | ||
| (c == ']' && st.top() != '[')) { | ||
| return false; | ||
| } | ||
| st.pop(); | ||
| } | ||
| } | ||
| return st.empty(); | ||
| } | ||
| }; |
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,23 @@ | ||
| class Solution { | ||
| public: | ||
| bool isValid(string s) { | ||
| unordered_map<char,char> map; | ||
| map['('] =')'; | ||
| map['{'] ='}'; | ||
| map['['] =']'; | ||
|
|
||
| stack<char> st; | ||
| for(char c : s){ | ||
| if(c=='(' || c=='{' || c=='['){ | ||
| st.push(c); | ||
| }else{ | ||
| if(st.empty() || | ||
| map[st.top()] != c){ | ||
| return false; | ||
| } | ||
| st.pop(); | ||
| } | ||
| } | ||
| return st.empty(); | ||
| } | ||
| }; | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map을 사용했을 때와
스택으로 넣었을 때 메모리 차이가 어떤가요?
map 을 사용 안하고 스택에 괄호를 반대로 넣는건 어떤가요?
if (c == '(' ) st.push(')')이런 식입니다.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1MB 차이가 납니다.
map을 사용 안한 방식(Solution2)은 현택님이 하신것과 동일한 방법같아요..! 저는 열린 괄호와 닫힌 괄호가 상응하는지 비교했고 현택님은 열린괄호를 넣을 때 닫힌 괄호도 같이 넣음으로써 닫힌괄호가 같은지 비교하신듯요..!
근데 그렇게 하면 괄호가 많아지면 스택에 쌓이는 괄호들이 많아지지 않을까요???
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
괄호가 스택에 많이 쌓이는 건 어차피 다른것들도 마찬가지 아닐까요??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제방법은 닫힌괄호를 넣지 않고 이미 넣어진 열린괄호를 pop 시켜줍니다..!!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 제말은 순회하면서 열린괄호가 나오면 닫힌괄호를 스택에 넣고
순회하면서 닫힌 괄호가 나오면 pop해서 비교하는 방식을 이야기 한겁니다!