-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Sehwan]Week2 solutions with JavaScript #58
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
Changes from all commits
e42b80c
4468d02
e704e3e
8df62a4
9c3e0e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 {TreeNode} | ||
*/ | ||
var invertTree = function (root) { | ||
// Check root is null | ||
if (!root) return null; | ||
// Create left and right variable to make recurrsive | ||
let left = root.left; | ||
let right = root.right; | ||
// Execute invertTree functino | ||
root.left = invertTree(right); | ||
root.right = invertTree(left); | ||
return root; | ||
}; | ||
|
||
// TC: O(n) | ||
// SC: O(n) |
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) { | ||
// Make fast pointer | ||
// Fast pointer will move two steps further inside of list | ||
let fast = head; | ||
// Iterate until fast pointer and head is equal | ||
while (fast && fast.next) { | ||
head = head.next; | ||
fast = fast.next.next; | ||
if (head == fast) return true; | ||
} | ||
return false; | ||
}; | ||
|
||
// TC: O(n) | ||
// SC: O(1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* 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) { | ||
// Make sure both or one of the list is null | ||
if (!list1 && !list2) return null; | ||
if (!list1) return list2; | ||
if (!list2) return list1; | ||
|
||
// Create dummy listNode | ||
let dummy = new ListNode(0); | ||
let head = dummy; | ||
// Make head of dummy list by using smaller node from list1 and list2 | ||
while (list1 && list2) { | ||
if (list1.val <= list2.val) { | ||
dummy.next = list1; | ||
list1 = list1.next; | ||
} else { | ||
dummy.next = list2; | ||
list2 = list2.next; | ||
} | ||
// After choosing with dummy list, head should be moved next | ||
dummy = dummy.next; | ||
} | ||
// Iterate until both of list head is equal to null | ||
if (list1 !== null) { | ||
dummy.next = list1; | ||
} else { | ||
dummy.next = list2; | ||
} | ||
return head.next; | ||
}; | ||
|
||
// TC: O(m+n) | ||
// SC: O(1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* 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} | ||
*/ | ||
|
||
// Declare linked list | ||
function ListNode(val) { | ||
this.val = val; | ||
this.next = null; | ||
} | ||
|
||
var reverseList = function (head) { | ||
let prev = null; | ||
let curr = head; | ||
let next; | ||
|
||
while (curr !== null) { | ||
next = curr.next; | ||
curr.next = prev; | ||
prev = curr; | ||
curr = next; | ||
} | ||
return prev; | ||
}; | ||
|
||
// Create liked list nodes | ||
const node1 = new ListNode(1); | ||
const node2 = new ListNode(2); | ||
const node3 = new ListNode(3); | ||
const node4 = new ListNode(4); | ||
const node5 = new ListNode(5); | ||
|
||
node1.next = node2; | ||
node2.next = node3; | ||
node3.next = node4; | ||
node4.next = node5; | ||
|
||
// Print out linked list | ||
let current = node1; | ||
while (current !== null) { | ||
console.log(current.val); | ||
current = current.next; | ||
} | ||
|
||
// Reversed linked list | ||
const reversedHead = reverseList(node1); | ||
|
||
// Print out reversed linked list | ||
current = reversedHead; | ||
while (current !== null) { | ||
console.log(current.val); | ||
current = current.next; | ||
} | ||
|
||
// TC: O(n) | ||
// SC: O(1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
var isValid = function (s) { | ||
// Create stack array | ||
let stack = []; | ||
// Iterate string to find there is an open bracket | ||
for (char of s) { | ||
// If it is an open bracket, push the close bracket into stack array | ||
if (char === "(") stack.push(")"); | ||
else if (char === "{") stack.push("}"); | ||
else if (char === "[") stack.push("]"); | ||
// If it is not an open bracket, compare last element of stack array | ||
else if (char !== stack.pop()) return false; | ||
Comment on lines
+10
to
+15
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. λ³΄ν΅ μ¬λ κ΄νΈλ₯Ό μ€νμ λ£κ³ , λ«λ κ΄νΈκ° λμ¬ λ, μ¬λ κ΄νΈλ₯Ό μ€νμμ κΊΌλ΄μ λμ΄ μμ΄ λ§λμ§λ₯Ό νμΈνλλ°μ. μ΄λ κ² μ²μλΆν° μμ΄ λ§λ λ«λ κ΄νΈλ₯Ό λ£μ΄λλ κ²λ μ μ ν μ κ·Όμ΄λ€μ! μμ£Ό μ’μ΅λλ€. π 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. κ°μ¬ν©λλ€. π |
||
} | ||
// Check stack array still has any element after iteration | ||
return stack.length === 0; | ||
}; | ||
|
||
// TC: O(n) | ||
// MC: O(n) | ||
|
||
console.log(isValid("()")); //true | ||
console.log(isValid("()[]{}")); //true | ||
console.log(isValid("([)]")); //false |
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.
μ€μ°... ν μ€νΈκΉμ§ ν΄λ³΄μ ¨κ΅°μ π
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.
λ€λ€ μ΄λ κ² λ§λ€μ΄λ³΄λκΉ λ§ν¬λ 리μ€νΈμ λν μ΄ν΄λκ° μ‘°κΈ λ μ¬λΌκ°λ κ² κ°μμ γ γ