-
Notifications
You must be signed in to change notification settings - Fork 1
[WEEK05-2] 최준호 #20
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
[WEEK05-2] 최준호 #20
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} a | ||
| * @param {string} b | ||
| * @return {string} | ||
| */ | ||
| var addBinary = function (a, b) { | ||
| const aLen = a.length; | ||
| const bLen = b.length; | ||
| const len = aLen > bLen ? aLen : bLen; | ||
| let num = 0; | ||
| const arr = []; | ||
|
|
||
| for (let i = 1; i <= len; i++) { | ||
| if (a.at(-i) === "1" && b.at(-i) === "1") { | ||
| arr.push(0 + num); | ||
| num = 1; | ||
| } else if (a.at(-i) === "1" || b.at(-i) === "1") { | ||
| if (num === 1) { | ||
| arr.push(0); | ||
| num = 1; | ||
| } else { | ||
| arr.push(1); | ||
| num = 0; | ||
| } | ||
| } else { | ||
| if (num === 1) { | ||
| arr.push(1); | ||
| num = 0; | ||
| } else { | ||
| arr.push(0); | ||
| num = 0; | ||
| } | ||
| } | ||
| } | ||
|
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. 요런 예외케이스 처리를 잘하시는군요 👍 !! |
||
|
|
||
| return (num === 1 ? "1" : "") + arr.reverse().join(""); | ||
| }; | ||
|
|
||
| /* | ||
| 26분 걸림. | ||
|
|
||
| 시간 복잡도는 O(n)이다. | ||
|
|
||
| 문자열을 뒤에서부터 순회하면서 풀이했다. | ||
| a와 b의 길이를 구해서, 더 긴 문자열의 길이만큼 루프를 돌면서 풀이했다. | ||
| a와 b의 각 자리 숫자를 비교하면서, 1과 1이 나오면 0을 arr에 push하고 num을 1로 업데이트했다. | ||
| 1과 0이 나오면, num이 1이면 arr에 0을 push하고 num을 1로 업데이트했다. | ||
| num이 0이면 arr에 1을 push하고 num을 0으로 업데이트했다. | ||
| 0과 0이 나오면, num이 1이면 arr에 1을 push하고 num을 0으로 업데이트했다. | ||
| num이 0이면 arr에 0을 push하고 num을 0으로 업데이트했다. | ||
| 루프가 끝난 후에, num이 1이면 "1"을 반환값의 앞에 붙이고, arr를 뒤집어서 문자열로 만들어서 반환한다. | ||
|
|
||
| 풀이가 생각보다 어려웠다. 십진수를 이진수로 이진수를 십진수로 변환하는 함수를 만들어서 풀어도 될 것 같다. | ||
| 배열에 push하는 방식말고 뭔가 다른 방식으로 풀이할 수 있을 것 같은데 생각이 나지 않는다. | ||
| */ | ||
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,50 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * function TreeNode(val, left, right) { | ||
| * this.val = (val===undefined ? 0 : val) | ||
| * this.left = (left===undefined ? null : left) | ||
| * this.right = (right===undefined ? null : right) | ||
| * } | ||
| */ | ||
| /** | ||
| * @param {TreeNode} root | ||
| * @return {number} | ||
| */ | ||
| var diameterOfBinaryTree = function (root) { | ||
| let maxLevel = 0; | ||
|
|
||
| const dfs = (node, level) => { | ||
| if (!node) { | ||
| if (maxLevel < level) maxLevel = level; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (node.left) dfs(node.left, level + 1); | ||
| if (node.right) dfs(node.right, level + 1); | ||
| }; | ||
|
|
||
| dfs(root.right, 0); | ||
|
|
||
| const tmp = maxLevel; | ||
| maxLevel = 0; | ||
|
|
||
| dfs(root.left, 0); | ||
|
|
||
| return tmp + maxLevel; | ||
| }; | ||
|
|
||
| /* | ||
| 문제 풀지 못함. | ||
|
|
||
| 시간 복잡도는 O(n)이다. | ||
|
|
||
| DFS 알고리즘을 이용하여 풀이했다. | ||
|
|
||
| DFS를 이용해서 문제를 풀려고 했다. | ||
| DFS를 왼쪽 오른쪽을 구분해서 풀려고 했다. | ||
| 접근이 완전히 잘못됐다. | ||
| 한쪽으로만 DFS가 될수도 있는 상황이다. | ||
| 사실 양쪽으로 나눠서 구하는 방식도 제대로 구현하지 못했다. | ||
|
|
||
| */ |
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.
전 조건으로 처리하려다가 점점 꼬여서 결국 못풀었는데 잘하시네요! a.at(-i), b.at(-i)를 활용해서 길이가 다른 경우도 자연스럽게 처리한 부분 좋은것 같습니다. 다만 분기가 조금 많은 편이라 aBit, bBit, sum처럼 중간 변수를 두면 로직이 더 읽기 쉬워질 것 같아요!