Skip to content

Commit b43de0f

Browse files
authored
Merge pull request #66 from saysimple0828/week2-saysimple
[saysimple] 2주차 문제 풀이입니다.
2 parents 78c7981 + 23fd800 commit b43de0f

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

invert-binary-tree/saysimple.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
# TC, SC: O(n), O(n)
8+
class Solution:
9+
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
10+
if not root:
11+
return None
12+
13+
left = root.left
14+
root.left = root.right
15+
root.right = left
16+
17+
self.invertTree(root.left)
18+
self.invertTree(root.right)
19+
20+
return root

linked-list-cycle/saysimple.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
# TC, SC: O(n), O(1)
7+
class Solution:
8+
def hasCycle(self, head: Optional[ListNode]) -> bool:
9+
if not head:
10+
return None
11+
12+
head.pos = 0
13+
14+
while head.next:
15+
if hasattr(head.next, "pos"):
16+
return True
17+
head.next.pos = head.pos + 1
18+
head = head.next
19+
20+
return False

merge-two-sorted-lists/saysimple.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
# TC, SC: O(n), O(n)
7+
class Solution:
8+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
9+
if not (list1 and list2):
10+
return list1 if list1 else list2
11+
12+
ret = ListNode(0, None)
13+
head = ret
14+
15+
while list1 and list2:
16+
a, b = list1.val, list2.val
17+
print(a, b)
18+
19+
if a < b:
20+
print(1)
21+
next = ListNode(a, None)
22+
ret.next = next
23+
ret = next
24+
list1 = list1.next
25+
elif a > b:
26+
print(2)
27+
next = ListNode(b, None)
28+
ret.next = next
29+
ret = next
30+
list2 = list2.next
31+
else:
32+
for i in range(2):
33+
next = ListNode(a, None)
34+
ret.next = next
35+
ret = next
36+
list1, list2 = list1.next, list2.next
37+
print(head)
38+
39+
if list1:
40+
while list1:
41+
next = ListNode(list1.val, None)
42+
ret.next = next
43+
ret = next
44+
list1 = list1.next
45+
46+
if list2:
47+
while list2:
48+
next = ListNode(list2.val, None)
49+
ret.next = next
50+
ret = next
51+
list2 = list2.next
52+
53+
head = head.next
54+
55+
return head

reverse-linked-list/saysimple.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
# TC, SC: O(n), O(1)
7+
class Solution:
8+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
9+
if not head:
10+
return None
11+
12+
if not head.next:
13+
return head
14+
15+
prev_node = head
16+
node = prev_node.next
17+
prev_node.next = None
18+
19+
while node.next:
20+
next_node = node.next
21+
node.next = prev_node
22+
prev_node = node
23+
node = next_node
24+
25+
node.next = prev_node
26+
27+
return node

valid-parentheses/saysimple.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# TC, SC: O(n), O(n)
2+
class Solution:
3+
def isValid(self, s: str) -> bool:
4+
arr = []
5+
6+
for c in s:
7+
if c in ["(", "[", "{"]:
8+
arr.append(c)
9+
continue
10+
else:
11+
if not arr:
12+
return False
13+
b = arr.pop()
14+
if b == "(" and c == ")":
15+
continue
16+
if b == "[" and c == "]":
17+
continue
18+
if b == "{" and c == "}":
19+
continue
20+
return False
21+
if arr:
22+
return False
23+
else:
24+
return True

0 commit comments

Comments
 (0)