-
-
Notifications
You must be signed in to change notification settings - Fork 245
[youngduck] WEEK 06 solutions #1861
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
var isValid = function (s) { | ||
// stack문제. open,close에 따라 스택처리 | ||
// 최종 stack 비워져있는지 마지막 확인까지. | ||
const stack = []; | ||
// open,close Bracket index 위치로 짝궁 판단할예정 | ||
const openBracket = ['(', '[', '{']; | ||
const closeBracket = [')', ']', '}']; | ||
// 반환할 결과값 flag | ||
let result = true; | ||
|
||
[...s].map((item) => { | ||
// indexOf자체는 선형검색이라 O(n)이지만 데이터 양이 3으로 고정되어있으므로 O(1)로 볼 수 있음 | ||
const itemIndexAtOpenBracket = openBracket.indexOf(item); | ||
const itemIndexAtCloseBracket = closeBracket.indexOf(item); | ||
// openBracket 타입 | ||
if (itemIndexAtOpenBracket > -1) { | ||
// push,pop 모두 O(1) 시간복잡도를 가짐. 끝에 요소 추가제거이므로 | ||
stack.push(item); | ||
} | ||
// clseBracket 타입 | ||
else if (itemIndexAtCloseBracket > -1) { | ||
const target = stack.pop(); | ||
if (target !== openBracket[itemIndexAtCloseBracket]) { | ||
result = false; | ||
} | ||
} | ||
}); | ||
|
||
if (stack.length !== 0) { | ||
result = false; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
// 시간복잡도 : O(n) | ||
// 공간복잡도 : O(n) |
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
으로 구현하니까 더 편하더라구요 !