-
-
Notifications
You must be signed in to change notification settings - Fork 245
[Leo] 2nd Week solutions #53
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
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,17 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: | ||
if not root: | ||
return None | ||
|
||
else: | ||
root.right, root.left = self.invertTree(root.left), self.invertTree(root.right) | ||
|
||
return root | ||
|
||
## TC: O(n), SC: O(n), avg O(logn) if the given tree is balanced | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.next = None | ||
|
||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
slow = head | ||
fast = head | ||
|
||
while fast and fast.next: | ||
slow = slow.next | ||
fast = fast.next.next | ||
if slow == fast: | ||
return True | ||
|
||
return False | ||
|
||
# visited = head | ||
|
||
# while visited: | ||
# if visited.val == None: | ||
# return True | ||
|
||
# visited.val = None | ||
# visited = visited.next | ||
|
||
# return False | ||
|
||
# Both TC:O(n) and SC:O(1), but below one is kinda tricky solution |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
class Solution: | ||
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: | ||
if not list1 or not list2: | ||
return list1 or list2 | ||
|
||
if list1.val <= list2.val: | ||
list1.next = self.mergeTwoLists(list1.next, list2) | ||
return list1 | ||
|
||
else: | ||
list2.next = self.mergeTwoLists(list1, list2.next) | ||
return list2 | ||
|
||
## TC: O(n) or O(n+m), depends on the length of list1 and list2 | ||
## SC: O(max(m,n)) |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,18 @@ | ||||||||||||||||
# Definition for singly-linked list. | ||||||||||||||||
# class ListNode: | ||||||||||||||||
# def __init__(self, val=0, next=None): | ||||||||||||||||
# self.val = val | ||||||||||||||||
# self.next = next | ||||||||||||||||
class Solution: | ||||||||||||||||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||||||||||||||||
prev, curr = None, head | ||||||||||||||||
|
||||||||||||||||
while curr: | ||||||||||||||||
tmp = curr.next ## save next node | ||||||||||||||||
curr.next = prev ## reverse next pointer to the prev | ||||||||||||||||
prev = curr ## update prev pointer with curr, since it's reversed now | ||||||||||||||||
curr = tmp ## move on to the next saved node | ||||||||||||||||
Comment on lines
+10
to
+14
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.
Suggested change
tmp 사용 없이 이렇게 하는 방법도 있을 것 같아요. 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. 감사합니다!! 포인터 문제들은 늘 할때마다 헷갈려서 tmp 해놓은거도 있고 안해놓은거도 있고 뒤죽박죽이네요 ㅎㅎ 좀 일관성있게 작성하도록 노력해보겠습니다 😄 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. 오 파이썬은 temp 없이 스왑이 되는군요...! |
||||||||||||||||
|
||||||||||||||||
return prev | ||||||||||||||||
|
||||||||||||||||
## TC: O(n) SC: O(1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
|
||
d = {'(': ')', '[': ']', '{': '}'} | ||
stack = [] | ||
|
||
for i in s: | ||
if i in d: | ||
stack.append(i) | ||
elif len(stack) == 0 or d[stack.pop()] != i: | ||
return False | ||
|
||
return len(stack) == 0 | ||
|
||
## O(n) time complexity but not that much fast by leetcode system... | ||
## Space complexity is also O(n) |
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.
훌륭한 복잡도 분석이네요! 💯