-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #831 from rivkode/main
[rivkode] Week4
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,53 @@ | ||
class ListNode(object): | ||
def __init__(self, val=0, next=None): | ||
self.val = val | ||
self.next = next | ||
|
||
class Solution(object): | ||
def mergeTwoLists(self, list1, list2): | ||
""" | ||
:type list1: Optional[ListNode] | ||
:type list2: Optional[ListNode] | ||
:rtype: Optional[ListNode] | ||
""" | ||
# 더미 노드 생성 | ||
dummy = ListNode(-1) | ||
current = dummy | ||
|
||
# 두 리스트를 순회하며 병합 | ||
while list1 and list2: | ||
if list1.val <= list2.val: | ||
current.next = list1 | ||
list1 = list1.next | ||
else: | ||
current.next = list2 | ||
list2 = list2.next | ||
current = current.next | ||
|
||
# 남아 있는 노드 처리 | ||
if list1: | ||
current.next = list1 | ||
elif list2: | ||
current.next = list2 | ||
|
||
# 더미 노드 다음부터 시작 | ||
return dummy.next | ||
|
||
if __name__ == "__main__": | ||
solution = Solution() | ||
|
||
# test case | ||
list1 = ListNode(1, ListNode(2, ListNode(4, ))) | ||
list2 = ListNode(1, ListNode(3, ListNode(4, ))) | ||
|
||
result = solution.mergeTwoLists(list1, list2) | ||
|
||
while result is not None: | ||
print(result.val) | ||
result = result.next | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains 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,38 @@ | ||
class Solution(object): | ||
# 시간복잡도 nlog(n) sort | ||
# 공간복잡도 n 정렬시 | ||
def missingNumber_1(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: int | ||
""" | ||
sort_nums = sorted(nums) | ||
|
||
v = 0 | ||
for i in sort_nums: | ||
if v != i: | ||
return v | ||
else: | ||
v += 1 | ||
|
||
return v | ||
|
||
# hash를 사용 | ||
# 모든 숫자를 dict에 입력 | ||
# for문을 돌면서 해당 hash가 dict에 존재하는지 체크 | ||
# 시간복잡도 n - for문 | ||
# 공간복잡도 n - dict 생성 | ||
def missingNumber(self, nums): | ||
hash_keys = dict() | ||
for i in range(len(nums) + 1): | ||
hash_keys[i] = 0 | ||
for i in nums: | ||
hash_keys[i] = 1 | ||
|
||
for i in range(len(nums) + 1): | ||
if hash_keys[i] != 1: | ||
return i | ||
|
||
return 0 | ||
|
||
|