-
-
Notifications
You must be signed in to change notification settings - Fork 305
[rivkode] WEEK 06 solutions #2205
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
rivkode
wants to merge
1
commit into
DaleStudy:main
Choose a base branch
from
rivkode:main
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.
+51
−0
Open
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,51 @@ | ||
| /* | ||
| 1. 문제 이해 | ||
| (){}[] 문자가 들어올때 순서대로 들어오는가 ? | ||
| 여기서 순서란 ([)] 와 같이 섞여 있는게 아니라 ([{}])와 같이 가운데를 기준으로 들어온것이 맞는지를 말하는 것 | ||
|
|
||
| 2. 알고리즘 | ||
| 스택을 사용한다 | ||
| 이유는 이 문제를 해결할때 마지막 입력과 다음 것을 비교해야하는데 | ||
| 가장 마지막에 입력된 것과 들어올 것을 서로 비교하기 가장 적합한 자료구조이기 때문이다. | ||
|
|
||
| 3. 예외 | ||
| 1개만 요소가 존재한다면 ? | ||
|
|
||
| 4. 구현 | ||
|
|
||
| 각 요소에 대해 hash map 을 생성한다 | ||
| 스택을 두고 첫번째 값을 넣는다 | ||
| 그 다음부터는 순차적으로 비교하며 동일할 경우 다음 값과 마지막에 있던 값을 제거한다. | ||
|
|
||
|
|
||
| */ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public boolean isValid(String s) { | ||
| Map<Character, Character> parens = new HashMap<>(); | ||
| parens.put('(', ')'); | ||
| parens.put('{', '}'); | ||
| parens.put('[', ']'); | ||
| Stack<Character> stack = new Stack<>(); | ||
|
|
||
| for (int i=0; i<s.length(); i++) { | ||
| char cur = s.charAt(i); | ||
|
|
||
| if (parens.containsKey(cur)) { | ||
| stack.push(cur); | ||
| } else { | ||
| if (stack.isEmpty() || cur != parens.get(stack.pop())) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (s.length() == 1 || !stack.isEmpty()) { | ||
|
Contributor
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. [작은 개선 포인트] // 기존
if (s.length() == 1 || !stack.isEmpty()) {
return false;
}
return true;
return stack.isEmpty(); |
||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
|
|
||
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.
전체적으로 주석에 사고 과정도 잘 정리되어 있고, 스택의 LIFO 특성을 활용해야 하는 이유도 명확하게 이해하고 계시네요! 👍