-
Notifications
You must be signed in to change notification settings - Fork 1
[WEEK07-1] 최준호 #30
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
[WEEK07-1] 최준호 #30
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,55 @@ | ||
| /** | ||
| * @param {string} s | ||
| * @param {string} t | ||
| * @return {boolean} | ||
| */ | ||
| var backspaceCompare = function (s, t) { | ||
| const stack1 = []; | ||
| const stack2 = []; | ||
|
|
||
| for (let i = 0; i < s.length; i++) { | ||
| const char = s[i]; | ||
|
|
||
| if (s[i] === "#") { | ||
| if (stack1.length) { | ||
| stack1.pop(); | ||
| } | ||
| } else { | ||
| stack1.push(char); | ||
| } | ||
| } | ||
|
|
||
| for (let i = 0; i < t.length; i++) { | ||
| const char = t[i]; | ||
|
|
||
| if (t[i] === "#") { | ||
| if (stack2.length) { | ||
| stack2.pop(); | ||
| } | ||
| } else { | ||
| stack2.push(char); | ||
| } | ||
| } | ||
|
|
||
| return stack1.join("") === stack2.join(""); | ||
| }; | ||
|
Member
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. 반복되는 풀이가 있어서 함수로 따로 빼도 좋을 것 같아요 👍 |
||
|
|
||
| /* | ||
| 16분 걸림. | ||
|
|
||
| 시간 복잡도는 O(n + m)이다. n과 m은 각각 s와 t의 길이이다. | ||
| 공간 복잡도는 O(n + m)이다. stack1과 stack2의 크기는 각각 s와 t의 길이까지 될 수 있기 때문이다. | ||
|
|
||
| stack을 이용해서 문제를 풀었다. | ||
| stack에 문자를 넣다가 #이 나오면 stack에서 pop을 해주었다. | ||
| stack1과 stack2를 만들어서 각각 s와 t를 처리해주었다. | ||
| 마지막에 stack1과 stack2를 문자열로 만들어서 비교해주었다. | ||
|
|
||
|
|
||
| 처음에 문제 이해하는데 시간이 좀 걸렸다. | ||
| backspace를 space로 착각했다. | ||
| 문제를 제대로 이해하고 나서 stack을 이용해서 문제를 풀었다. | ||
|
|
||
| O(n)의 풀이를 보면 뒤에서부터 비교해가면서 #이 나오면 #과 그 앞의 문자를 건너뛰는 방식으로 풀이한다. | ||
| while 문을 중첩을 하는데 O(n)으로 나오는 걸 보고 | ||
| */ | ||
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,34 @@ | ||
| /** | ||
| * @param {number} n | ||
| * @return {number[]} | ||
| */ | ||
| var countBits = function (n) { | ||
| const result = []; | ||
|
|
||
| for (let i = 0; i <= n; i++) { | ||
| const len = i | ||
| .toString(2) | ||
| .split("") | ||
| .filter((num) => num === "1") | ||
| .join("").length; | ||
|
|
||
|
Collaborator
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. toString, split, filter, join 등 메서드 잘 활용하시는거같네요 ㅎㅎ |
||
| result.push(len); | ||
| } | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| /* | ||
| 7분 걸림. | ||
|
|
||
| 시간 복잡도는 O(nlogn)이다. n은 입력값이다. | ||
| 공간 복잡도는 O(n)이다. result 배열의 크기는 n + 1이기 때문이다. | ||
|
|
||
| 0부터 n까지의 수를 2진수로 바꾼다. | ||
| 2진수 문자열을 split으로 나누고 filter로 1인 것만 남긴다. | ||
| 남은 문자열을 join으로 합쳐서 길이를 구한다. | ||
| 길이를 result 배열에 push한다. | ||
|
|
||
| 많은 메서드를 이용해서 풀이를 했는데 비트연산을 이용한 점화식 풀이를 보았다. | ||
| 많은 지식을 알고 있으면 풀이 방법이 다양해지는 것 같다. | ||
| */ | ||
Oops, something went wrong.
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.
스택을 활용한 정석 풀이로 잘 구현하신 것 같네요, 이미 말씀해주신 부분이긴하지만, s, t를 처리하는 로직이 거의 동일해서 보조 함수로 분리하면 더 깔끔할 것 같아요!!