-
Notifications
You must be signed in to change notification settings - Fork 126
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
[rivkode] Week4 #831
Merged
Merged
[rivkode] Week4 #831
Changes from all commits
Commits
Show all changes
3 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 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 | ||
|
||
|
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.
@taewanseoul 안녕하세요 ! 리뷰 남겨주셔서 감사합니다.
혹시 그럼 남아있는 노드를 제거 하기 위해서 저 if else 부분을 while문에 같이 넣는 방법일까요 ?
로직 순서를 말씀하신건지 혹은 아예 저 남아있는 노드 처리 부분을 없애고 다른 방법을 찾아보는건지 궁금합니다 ㅎㅎ