Skip to content

Commit 481f6e2

Browse files
committed
week 3
1 parent 75721fc commit 481f6e2

File tree

11 files changed

+433
-0
lines changed

11 files changed

+433
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
working

1.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#! /usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
:w
5+
46

57
class Solution(object):
68
def twoSum(self, nums, target):

18.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def fourSum(self, nums, target):
7+
"""
8+
:type nums: List[int]
9+
:type target: int
10+
:rtype: List[List[int]]
11+
"""
12+
nums = sorted(nums)
13+
solution = []
14+
length = len(nums)
15+
16+
for i in range(length - 3):
17+
a = nums[i]
18+
for j in range(i + 1, length - 2):
19+
b = nums[j]
20+
21+
# Two points which are form head and bottom move toward
22+
# to make the a + b + c + d == target
23+
left = j + 1
24+
right = length - 1
25+
while left < right:
26+
c = nums[left]
27+
d = nums[right]
28+
if a + b + c + d < target:
29+
left += 1
30+
elif a + b + c + d > target:
31+
right -= 1
32+
else:
33+
solution.append([a, b, c, d])
34+
left += 1
35+
right -= 1
36+
37+
# remove the duplicate list
38+
no_duplicate = []
39+
for terms in solution:
40+
if terms not in no_duplicate:
41+
no_duplicate.append(terms)
42+
43+
return no_duplicate

19.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# Definition for singly-linked list.
5+
# class ListNode(object):
6+
# def __init__(self, x):
7+
# self.val = x
8+
# self.next = None
9+
10+
11+
class Solution(object):
12+
def removeNthFromEnd(self, head, n):
13+
"""
14+
:type head: ListNode
15+
:type n: int
16+
:rtype: ListNode
17+
"""
18+
counter = 1 # The length of the list
19+
head_counter = head
20+
# Get te length of the list
21+
while head_counter.next:
22+
counter += 1
23+
head_counter = head_counter.next
24+
25+
count_from_head = 1
26+
head_keep = head
27+
while head.next:
28+
# The next node will be removed
29+
if count_from_head == counter - n:
30+
head.next = head.next.next
31+
break
32+
head = head.next
33+
count_from_head += 1
34+
35+
# the node removed is the head.
36+
if counter == n:
37+
return head_keep.next
38+
39+
else:
40+
return head_keep

20.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def isValid(self, s):
7+
"""
8+
:type s: str
9+
:rtype: bool
10+
"""
11+
close_s = ""
12+
for symbol in s:
13+
if symbol == "(":
14+
close_s = ")" + close_s
15+
16+
elif symbol == "[":
17+
close_s = "]" + close_s
18+
19+
elif symbol == "{":
20+
close_s = "}" + close_s
21+
22+
elif symbol == ")":
23+
if close_s and close_s[0] == ")":
24+
close_s = close_s[1:]
25+
continue
26+
else:
27+
return False
28+
elif symbol == "]":
29+
if close_s and close_s[0] == "]":
30+
close_s = close_s[1:]
31+
continue
32+
else:
33+
return False
34+
else:
35+
if close_s and close_s[0] == "}":
36+
close_s = close_s[1:]
37+
continue
38+
else:
39+
return False
40+
if close_s == "":
41+
return True
42+
else:
43+
return False

21.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# Definition for singly-linked list.
5+
# class ListNode(object):
6+
# def __init__(self, x):
7+
# self.val = x
8+
# self.next = None
9+
10+
11+
class Solution(object):
12+
def mergeTwoLists(self, l1, l2):
13+
"""
14+
:type l1: ListNode
15+
:type l2: ListNode
16+
:rtype: ListNode
17+
"""
18+
while not l1 and not l2:
19+
return None
20+
21+
merged_list = ListNode(0)
22+
merged_head = merged_list
23+
24+
# Scan through l1 and l2, get the smaller one to merged list
25+
while l1 and l2:
26+
if l1.val <= l2.val:
27+
node = ListNode(l1.val)
28+
l1 = l1.next
29+
else:
30+
node = ListNode(l2.val)
31+
l2 = l2.next
32+
merged_list.next = node
33+
merged_list = merged_list.next
34+
35+
# l2 has gone to the tail already and only need to scan l1
36+
while l1:
37+
node = ListNode(l1.val)
38+
l1 = l1.next
39+
merged_list.next = node
40+
merged_list = merged_list.next
41+
42+
# l1 has gone to the tail already and only need to scan l2
43+
while l2:
44+
node = ListNode(l2.val)
45+
l2 = l2.next
46+
merged_list.next = node
47+
merged_list = merged_list.next
48+
49+
return merged_head.next

22.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
def generateParenthesis(self, n):
6+
"""
7+
:type n: int
8+
:rtype: List[str]
9+
"""
10+
11+
# node = [leftchild, rightchild, count"(", count")", current str]
12+
if not n:
13+
return [""]
14+
solution = []
15+
root = [None, None, 1, 0, "("]
16+
self.generate_child_tree(root, n, solution)
17+
return solution
18+
19+
20+
def generate_child_tree(self, node, n, solution):
21+
# the node is the leave and the str is what we want
22+
if node[2] == node[3] == n:
23+
node[0] = None
24+
node[1] = None
25+
solution.append(node[4])
26+
27+
# the node have both left and right child
28+
elif node[2] > node[3] and node[2] < n:
29+
left_child = [None, None, node[2] + 1, node[3], node[4] + "("]
30+
right_child = [None, None, node[2], node[3] + 1, node[4] + ")"]
31+
node[0] = left_child
32+
node[1] = right_child
33+
self.generate_child_tree(left_child, n, solution)
34+
self.generate_child_tree(right_child, n, solution)
35+
36+
# the node have only left child
37+
elif node[2] == node[3] and node[2] < n:
38+
left_child = [None, None, node[2] + 1, node[3], node[4] + "("]
39+
node[0] = left_child
40+
self.generate_child_tree(left_child, n, solution)
41+
42+
# the node have only left child
43+
else:
44+
right_child = [None, None, node[2], node[3] + 1, node[4] + ")"]
45+
node[1] = right_child
46+
self.generate_child_tree(right_child, n, solution)

23.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# Definition for singly-linked list.
5+
# class ListNode(object):
6+
# def __init__(self, x):
7+
# self.val = x
8+
# self.next = None
9+
10+
# Definition for singly-linked list.
11+
# class ListNode(object):
12+
# def __init__(self, x):
13+
# self.val = x
14+
# self.next = None
15+
16+
import heapq
17+
18+
19+
class Solution(object):
20+
def mergeKLists(self, lists):
21+
"""
22+
:type lists: List[ListNode]
23+
:rtype: ListNode
24+
"""
25+
if not lists:
26+
return []
27+
28+
head = merged_list = ListNode(0)
29+
heap_record = []
30+
# push head of all the linked list to heap
31+
for node in lists:
32+
if node:
33+
heap_record.append((node.val, node))
34+
heapq.heapify(heap_record)
35+
36+
# get the min val and push the node into heap
37+
while heap_record:
38+
min_node = heapq.heappop(heap_record)
39+
merged_list.next = min_node[1]
40+
merged_list = merged_list.next
41+
if min_node[1].next:
42+
next_node = min_node[1].next
43+
heapq.heappush(heap_record, (next_node.val, next_node))
44+
45+
return head.next

24.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
# Definition for singly-linked list.
6+
# class ListNode(object):
7+
# def __init__(self, x):
8+
# self.val = x
9+
# self.next = None
10+
11+
class Solution(object):
12+
def swapPairs(self, head):
13+
"""
14+
:type head: ListNode
15+
:rtype: ListNode
16+
"""
17+
result = before_node = ListNode(0)
18+
before_node.next = head
19+
20+
"""
21+
Given a situation : ... -> B -> | C -> D | -> E -> ...
22+
Node before C is swaped, and then we should swap C and D,
23+
so the result is : ... -> B -> | D -> C | -> E -> ...
24+
Assume B is before_node, C is head, then we need to:
25+
1. B.next = D ( B -> D, C -> D, D -> E)
26+
2. D.next = C ( B -> D, D -> C, -> E)
27+
3. C.next = E ( B -> D, D -> C, C -> E)
28+
29+
"""
30+
while head:
31+
if head.next:
32+
next_node = head.next.next # Keep E
33+
before_node.next = head.next # Step 1
34+
head.next.next = head # Step 2
35+
head.next = next_node # Step 3
36+
37+
# Scan next two node
38+
before_node = head
39+
head = head.next
40+
else:
41+
break
42+
return result.next

25.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# Definition for singly-linked list.
5+
# class ListNode(object):
6+
# def __init__(self, x):
7+
# self.val = x
8+
# self.next = None
9+
10+
11+
class Solution(object):
12+
def reverseKGroup(self, head, k):
13+
"""
14+
:type head: ListNode
15+
:type k: int
16+
:rtype: ListNode
17+
"""
18+
19+
if not head or k <= 1:
20+
return head
21+
22+
solution = ListNode(0)
23+
solution.next = head
24+
before_node = solution
25+
count = 0
26+
27+
# Get each k nodes and reverse all of them
28+
while head:
29+
count += 1
30+
if count % k == 0:
31+
after_node = head.next
32+
before_node = self.reverse_k_nodes(before_node, after_node)
33+
head = after_node
34+
else:
35+
head = head.next
36+
37+
return solution.next
38+
39+
def reverse_k_nodes(self, before_node, after_node):
40+
"""
41+
Given a situation : ... -> B -> | C -> ... -> X | -> Y -> ...
42+
Nodes before C is swaped, and then we should swap node between C and X,
43+
so the result is : ... -> B -> | X -> ... -> C | -> Y -> ...
44+
Then what we need to is:
45+
1. Get node C and make it as tail in k-reversed-list: C
46+
2. Get all the other nodes and put each before the
47+
head of current k-reversed-list: X -> ... ->C
48+
3. Make B.next = X and C.next = Y:
49+
-> B -> | X -> ... -> C | -> Y
50+
"""
51+
if before_node.next == after_node:
52+
pass
53+
54+
# Step 2
55+
head = before_node.next
56+
reversed_list_head = reversed_list_tail = head
57+
cur_node = head.next
58+
while cur_node != after_node:
59+
keep_node = cur_node.next
60+
cur_node.next = reversed_list_head
61+
reversed_list_head = cur_node
62+
cur_node = keep_node
63+
64+
# Step 3
65+
before_node.next = reversed_list_head
66+
reversed_list_tail.next = after_node
67+
return reversed_list_tail

0 commit comments

Comments
 (0)