Skip to content

Commit a39d1b9

Browse files
committed
[main] Added generateParenthesis and merge sorted lists and swap pairs
1 parent 1ab0bb0 commit a39d1b9

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
3+
class Solution:
4+
def __init__(self):
5+
self.pairs = []
6+
7+
def generateParenthesis(self, n: int) -> List[str]:
8+
def generate_sub_pair(accumulator: str = '', open_count: int = 0, closed_count: int = 0) -> List[str]:
9+
if closed_count > open_count:
10+
return ['']
11+
if open_count == n and closed_count == n:
12+
return [accumulator]
13+
if (open_count + closed_count) > (n * 2):
14+
return ['']
15+
return generate_sub_pair(accumulator + '(', open_count + 1, closed_count) + generate_sub_pair(accumulator + ')', open_count, closed_count + 1)
16+
return [x for x in generate_sub_pair() if x != '']
17+
18+
19+
if __name__ == '__main__':
20+
sol = Solution()
21+
print(sol.generateParenthesis(3))
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Definition for singly-linked list.
2+
from typing import List, Optional, Tuple, Union
3+
4+
5+
class ListNode:
6+
def __init__(self, val=0, next=None):
7+
self.val: int = val
8+
self.next: Union[ListNode, None] = next
9+
10+
11+
class Solution:
12+
13+
def __init__(self):
14+
self.lists = []
15+
16+
def ret_node_value_and_next(self, index: int) -> Union[int, List]:
17+
val = self.lists[index].val if self.lists[index] is not None else None
18+
next = self.lists[index].next if self.lists[index] is not None else None
19+
if next:
20+
self.lists[index] = next
21+
else:
22+
self.lists[index] = None
23+
return val if val != None else []
24+
25+
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
26+
if len(self.lists) == 0:
27+
self.lists = lists
28+
nodes = []
29+
new_head = None
30+
loop = True
31+
while loop:
32+
result = [Solution.ret_node_value_and_next(
33+
self, ind) for ind in range(len(self.lists))]
34+
old_len = len(nodes)
35+
for eachValue in result:
36+
if eachValue != []:
37+
nodes.append(eachValue)
38+
if len(nodes) == old_len:
39+
break
40+
nodes.sort()
41+
tmp_head = None
42+
for eachNodeValue in nodes:
43+
if new_head is None:
44+
new_head = ListNode(eachNodeValue)
45+
tmp_head = new_head
46+
elif tmp_head is not None:
47+
tmp_head.next = ListNode(eachNodeValue)
48+
tmp_head = tmp_head.next
49+
return new_head
50+
51+
# def print_list(self, head: Union[ListNode, None]) -> None:
52+
# while head is not None:
53+
# print(head.val)
54+
# if head.next:
55+
# head = head.next
56+
# else:
57+
# break
58+
# print('Done!')
59+
60+
61+
if __name__ == '__main__':
62+
first_list = ListNode(0, ListNode(2, ListNode(5)))
63+
# second_list = ListNode(1, ListNode(3, ListNode(4)))
64+
# third_link = ListNode(2, ListNode(6))
65+
66+
sol = Solution()
67+
new_head = sol.mergeKLists([first_list])
68+
sol.print_list(new_head)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import Optional, Union
2+
3+
# Definition for singly-linked list.
4+
class ListNode:
5+
def __init__(self, val=0, next=None):
6+
self.val: int = val
7+
self.next: Union[ListNode, None] = next
8+
9+
10+
class Solution:
11+
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
12+
nodes = []
13+
while head is not None:
14+
nodes.append(head)
15+
head = head.next
16+
new_head = None
17+
temp_head = None
18+
for i in range(0, len(nodes) - 1, 2):
19+
nodes[i], nodes[i + 1], = nodes[i + 1], nodes[i]
20+
for ind, each_node in enumerate(nodes):
21+
if new_head is None:
22+
new_head = each_node
23+
temp_head = new_head
24+
else:
25+
temp_head.next = each_node
26+
temp_head = temp_head.next
27+
if ind == len(nodes) - 1:
28+
temp_head.next = None
29+
return new_head
30+
31+
def printList(self, head: Optional[ListNode]) -> None:
32+
while head is not None:
33+
print(head.val)
34+
head = head.next
35+
36+
37+
if __name__ == '__main__':
38+
alist = ListNode(1, ListNode(2, ListNode(3, ListNode(4))))
39+
sol = Solution()
40+
head = sol.swapPairs(alist)
41+
sol.printList(head)

0 commit comments

Comments
 (0)