-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Evan] Week 2 Solutions #59
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
40ee4bd
solve: 20. Valid Parentheses
sounmind 1ea80be
solve: 141. Linked List Cycle
sounmind 3734e6a
solve: 206. Reverse Linked List
sounmind c2b59a4
solve: 21. Merge Two Sorted Lists
sounmind c45004c
refactor(mergeTwoLists)
sounmind f9d9929
solve: 226. Invert Binary Tree
sounmind 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,22 @@ | ||
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 {TreeNode} | ||
*/ | ||
var invertTree = function (root) { | ||
if (!root) { | ||
return null; | ||
} | ||
|
||
const originLeft = root.left; | ||
const originRight = root.right; | ||
|
||
root.right = invertTree(originLeft); | ||
root.left = invertTree(originRight); | ||
|
||
return root; | ||
}; |
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,27 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val) { | ||
* this.val = val; | ||
* this.next = null; | ||
* } | ||
*/ | ||
|
||
/** | ||
* @param {ListNode} head | ||
* @return {boolean} | ||
*/ | ||
var hasCycle = function (head) { | ||
let fastPointer = head; | ||
let slowPointer = head; | ||
|
||
while (fastPointer !== null && slowPointer !== null) { | ||
fastPointer = fastPointer?.next?.next; | ||
slowPointer = slowPointer?.next; | ||
|
||
if (fastPointer === slowPointer) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; |
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,30 @@ | ||
function ListNode(val, next) { | ||
this.val = val === undefined ? 0 : val; | ||
this.next = next === undefined ? null : next; | ||
} | ||
|
||
/** | ||
* @param {ListNode} list1 | ||
* @param {ListNode} list2 | ||
* @return {ListNode} | ||
*/ | ||
var mergeTwoLists = function (list1, list2) { | ||
const dummy = new ListNode(); | ||
let current = dummy; | ||
|
||
while (list1 !== null && list2 !== null) { | ||
if (list1.val < list2.val) { | ||
current.next = list1; | ||
list1 = list1.next; | ||
} else { | ||
current.next = list2; | ||
list2 = list2.next; | ||
} | ||
|
||
current = current.next; | ||
} | ||
|
||
current.next = list1 !== null ? list1 : list2; | ||
|
||
return dummy.next; | ||
}; |
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,25 @@ | ||
/** | ||
* 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 currentNode = head; | ||
let previousNode = null; | ||
|
||
while (currentNode !== null) { | ||
const nextNode = currentNode.next; | ||
|
||
currentNode.next = previousNode; | ||
previousNode = currentNode; | ||
currentNode = nextNode; | ||
} | ||
|
||
return previousNode; | ||
}; |
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 @@ | ||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
var isValid = function (s) { | ||
if (s.length % 2 !== 0) { | ||
return false; | ||
} | ||
|
||
const stack = []; | ||
const pair = { | ||
")": "(", | ||
"}": "{", | ||
"]": "[", | ||
}; | ||
|
||
for (let char of s) { | ||
if (pair[char] && stack.pop() !== pair[char]) { | ||
return false; | ||
} | ||
|
||
stack.push(char); | ||
} | ||
|
||
return stack.length === 0; | ||
}; |
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.
JS
은?.
연산자 통해서 깔끔하게 적을 수 있네요 👍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.
네네ㅎㅎ Optional chaining이라는 연산자입니다. 비교적 최근에 나온 스펙이죠...