Open
Conversation
| class Solution { | ||
| public: | ||
| bool isValid(string s) { | ||
| unordered_map<char,char> map; |
Collaborator
There was a problem hiding this comment.
map을 사용했을 때와
스택으로 넣었을 때 메모리 차이가 어떤가요?
map 을 사용 안하고 스택에 괄호를 반대로 넣는건 어떤가요?if (c == '(' ) st.push(')') 이런 식입니다.
Collaborator
Author
Collaborator
There was a problem hiding this comment.
괄호가 스택에 많이 쌓이는 건 어차피 다른것들도 마찬가지 아닐까요??
Collaborator
Author
There was a problem hiding this comment.
제방법은 닫힌괄호를 넣지 않고 이미 넣어진 열린괄호를 pop 시켜줍니다..!!!
Collaborator
There was a problem hiding this comment.
아 제말은 순회하면서 열린괄호가 나오면 닫힌괄호를 스택에 넣고
순회하면서 닫힌 괄호가 나오면 pop해서 비교하는 방식을 이야기 한겁니다!
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

Valid Parentheses
괄호가 유효한지 판단하는 문제이다.
괄호는 같은 타입의 괄호로만 닫히고 열린괄호는 이에 상응하는 닫힌 괄호가 있다.
()-> closed{}-> closed[]-> closed열린 괄호는 옳은 순서로 닫힌다.
({})-> closed{(})-> not closedApproach
스택에 괄호를 입력받은 순서대로 넣는다.
스택이 비어있는데 닫힌 괄호라면 -> false
스택의 top과 입력받은 괄호가 상응한다면 -> pop
그렇지 않다면 -> false
입력이 끝난 후 스택에 요소가 남아있다면 괄호가 짝지어지지 않았다는 뜻이므로 -> false
Code
Solution 1
Solution 2
if 문에 조건을 다는것이 번거로워
괄호를 unordered_map 을 이용해서 해보았다.