-
Notifications
You must be signed in to change notification settings - Fork 1
[WEEK06-1] 추슬기 #23
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
[WEEK06-1] 추슬기 #23
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0a34ae1
feat: Reverse Linked List 풀이 작성
doitchuu 2a6ca25
feat: Majority Element 풀이 작성
doitchuu bc1bf57
feat: Add Binary 풀이 작성
doitchuu 3985667
feat: Diameter of Binary Tree 풀이 작성
doitchuu b0903f3
feat: Middle of the Linked List 풀이 작성
doitchuu f2766bd
feat: Maximum Depth of Binary Tree 풀이 작성
doitchuu 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,23 @@ | ||
| /** | ||
| * @param {string} a | ||
| * @param {string} b | ||
| * @return {string} | ||
| */ | ||
| var addBinary = function(a, b) { | ||
| let i = a.length - 1; | ||
| let j = b.length - 1; | ||
| let carry = 0; | ||
| let result = ""; | ||
|
|
||
| while (i >= 0 || j >= 0 || carry) { | ||
| let sum = carry; | ||
|
|
||
| if (i >= 0) sum += Number(a[i--]); | ||
| if (j >= 0) sum += Number(b[j--]); | ||
|
|
||
| result = String(sum % 2) + result; | ||
| carry = Math.floor(sum / 2); | ||
| } | ||
|
|
||
| return result; | ||
| }; |
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,29 @@ | ||
| /** | ||
| * 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 diameter = 0; | ||
|
|
||
| function dfs(node) { | ||
| if (node === null) return 0; | ||
|
|
||
| const left = dfs(node.left); | ||
| const right = dfs(node.right); | ||
|
|
||
| diameter = Math.max(diameter, left + right); | ||
|
|
||
| return Math.max(left, right) + 1; | ||
| } | ||
|
|
||
| dfs(root); | ||
| return diameter; | ||
| }; |
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,18 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number} | ||
| */ | ||
| var majorityElement = function(nums) { | ||
| // 배열엔 항상 다수의 원소가 존재한다. | ||
| // 다만, n / 2개 이상인 다수의 원소를 반환한다. | ||
| const map = new Map(); | ||
| const majorElement = []; | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| map.set(nums[i], (map.get(nums[i]) || 0) + 1); | ||
|
|
||
| if (map.get(nums[i]) > nums.length / 2) { | ||
| return nums[i]; | ||
| } | ||
| } | ||
| }; |
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,26 @@ | ||
| /** | ||
| * 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 maxDepth = function(root) { | ||
| function dfs(node) { | ||
| if (!node) { | ||
| return 0; | ||
| } | ||
|
|
||
| const left = dfs(node.left); | ||
| const right = dfs(node.right); | ||
|
|
||
| return Math.max(left, right) + 1; | ||
| } | ||
|
|
||
| return dfs(root); | ||
| }; |
|
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. 오 이렇게 Map으로 인덱스와 노드를 저장해서 middle을 찾는 방식으로도 구현할 수 있군요 알아갑니다! |
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,28 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * function ListNode(val, next) { | ||
| * this.val = (val===undefined ? 0 : val) | ||
| * this.next = (next===undefined ? null : next) | ||
| * } | ||
| */ | ||
| /** | ||
| * @param {ListNode} head | ||
| * @return {ListNode} | ||
| */ | ||
| var middleNode = function(head) { | ||
| let index = 1; | ||
| let current = head; | ||
| const map = new Map(); | ||
|
|
||
| while (current) { | ||
| map.set(index, current); | ||
| current = current.next; | ||
| index++; | ||
| } | ||
|
|
||
| map.set(index, current); | ||
|
|
||
| let middle = Math.ceil(map.size / 2); | ||
|
|
||
| return map.get(middle); | ||
| }; |
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,23 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * function ListNode(val, next) { | ||
| * this.val = (val===undefined ? 0 : val) | ||
| * this.next = (next===undefined ? null : next) | ||
| * } | ||
| */ | ||
| /** | ||
| * @param {ListNode} head | ||
| * @return {ListNode} | ||
| */ | ||
| var reverseList = function(head) { | ||
| let prev = null; | ||
| let current = head; | ||
|
|
||
| while (current) { | ||
| let next = current.next; | ||
| current.next = prev; | ||
| prev = current; | ||
| current = next; | ||
| } | ||
| return prev; | ||
| }; |
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.
DFS 재귀로 최대 깊이를 구하는 정석 풀이로 깔끔하게 잘 작성하신 것 같습니다. null 처리와 Math.max(left, right) + 1 흐름도 명확해서 읽기 좋았어요.