-
-
Notifications
You must be signed in to change notification settings - Fork 245
[Wan] Week 4 #830
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
[Wan] Week 4 #830
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,52 @@ | ||
/** | ||
* 21. Merge Two Sorted Lists | ||
* You are given the heads of two sorted linked lists list1 and list2. | ||
* Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists. | ||
* Return the head of the merged linked list. | ||
* | ||
* https://leetcode.com/problems/merge-two-sorted-lists/description/ | ||
*/ | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* class ListNode { | ||
* val: number | ||
* next: ListNode | null | ||
* constructor(val?: number, next?: ListNode | null) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
* } | ||
*/ | ||
|
||
// O(n + m) time | ||
// O(1) space | ||
function mergeTwoLists( | ||
list1: ListNode | null, | ||
list2: ListNode | null | ||
): ListNode | null { | ||
if (!list1) return list2; | ||
if (!list2) return list1; | ||
|
||
let mergedListNode: ListNode; | ||
const val1 = list1.val; | ||
const val2 = list2.val; | ||
if (val1 > val2) { | ||
mergedListNode = new ListNode(val2); | ||
mergedListNode.next = mergeTwoLists(list1, list2.next); | ||
} else { | ||
mergedListNode = new ListNode(val1); | ||
mergedListNode.next = mergeTwoLists(list1.next, list2); | ||
} | ||
|
||
return mergedListNode; | ||
} | ||
|
||
class ListNode { | ||
val: number; | ||
next: ListNode | null; | ||
constructor(val?: number, next?: ListNode | null) { | ||
this.val = val === undefined ? 0 : val; | ||
this.next = next === undefined ? null : 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,20 @@ | ||
/** | ||
* 268. Missing Number | ||
* Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. | ||
* | ||
* https://leetcode.com/problems/missing-number/description/ | ||
*/ | ||
|
||
// O(n) time | ||
// O(1) space | ||
function missingNumber(nums: number[]): number { | ||
const n = nums.length; | ||
let total = (n * (n + 1)) / 2; | ||
|
||
let sum = 0; | ||
for (let i = 0; i < n; i++) { | ||
sum += nums[i]; | ||
} | ||
|
||
return total - sum; | ||
} |
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.
재귀 호출인 만큼 스택 공간이 필요할것으로 보여요
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.
list1과 list2이 있으니 O(n + m)이 되겠군요...!