Skip to content

Commit ad22910

Browse files
committed
160
1 parent cc7608b commit ad22910

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

binary_tree_inorder_traversal.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def inorderTraversal(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: List[int]
13+
"""
14+
stack = [root]
15+
result = []
16+
while stack:
17+
item = stack.pop()
18+
if item is None:
19+
continue
20+
if isinstance(item, TreeNode):
21+
if not item.left and not item.right:
22+
result.append(item.val)
23+
else:
24+
stack.append(item.right)
25+
stack.append(item.val)
26+
stack.append(item.left)
27+
else:
28+
result.append(item)
29+
return result
30+

dungeon_game.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution(object):
2+
def calculateMinimumHP(self, dungeon):
3+
"""
4+
:type dungeon: List[List[int]]
5+
:rtype: int
6+
"""
7+
if not dungeon:
8+
return 0
9+
N = len(dungeon)
10+
M = len(dungeon[0])
11+
matrix = []
12+
for i in range(N):
13+
row = [0]*M
14+
matrix.append(row)
15+
matrix[N-1][M-1] = 1 if dungeon[N-1][M-1]>=0 else -dungeon[N-1][M-1]+1
16+
for n in range(M+N-1, -1, -1):
17+
for x in range(N):
18+
y = n-x
19+
if y <0 or y>=M:
20+
continue
21+
candidates=[]
22+
if x+1<N:
23+
candidates.append(matrix[x+1][y])
24+
if y+1<M:
25+
candidates.append(matrix[x][y+1])
26+
if not candidates: continue
27+
min_need = min(candidates)
28+
if dungeon[x][y]>=0:
29+
if dungeon[x][y]>=min_need:
30+
matrix[x][y]=1
31+
else:
32+
matrix[x][y]=min_need-dungeon[x][y]
33+
else:
34+
matrix[x][y] = min_need-dungeon[x][y]
35+
return matrix[0][0]
36+

gas_station.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def canCompleteCircuit(self, gas, cost):
3+
"""
4+
:type gas: List[int]
5+
:type cost: List[int]
6+
:rtype: int
7+
"""
8+
diffs = [gas[i]-cost[i] for i in range(len(gas))]
9+
least = 0
10+
min_index = 0
11+
sum = 0
12+
for i, diff in enumerate(diffs):
13+
if sum <least:
14+
least = sum
15+
min_index = i
16+
sum = sum+diff
17+
if sum<0:
18+
return -1
19+
return min_index

partition_list.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def partition(self, head, x):
9+
"""
10+
:type head: ListNode
11+
:type x: int
12+
:rtype: ListNode
13+
"""
14+
cursor= head
15+
precursor = None
16+
head1 = tail1 = None
17+
head2 = tail2 = None
18+
19+
while cursor:
20+
if cursor.val < x:
21+
if not head1:
22+
head1 = cursor
23+
tail1 = cursor
24+
else:
25+
tail1.next = cursor
26+
tail1 = tail1.next
27+
if tail2:
28+
tail2.next = cursor.next
29+
else:
30+
if not head2:
31+
head2 = cursor
32+
tail2 = cursor
33+
cursor = cursor.next
34+
if tail1:
35+
tail1.next = None
36+
if tail2:
37+
tail2.next = None
38+
if not head1:
39+
return head2
40+
else:
41+
tail1.next = head2
42+
return head1
43+
44+
45+
46+
47+
48+
49+
50+
51+

swap_nodes_in_pairs.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def swapPairs(self, head):
9+
"""
10+
:type head: ListNode
11+
:rtype: ListNode
12+
"""
13+
if not head or not head.next:
14+
return head
15+
16+
newHead = None
17+
tail = None
18+
p1 = head
19+
p2 = head.next
20+
while p1 and p2:
21+
p1.next = p2.next
22+
p2.next = p1
23+
if newHead:
24+
tail.next = p2
25+
else:
26+
newHead = p2
27+
tail = p1
28+
29+
p1 = p1.next
30+
if p1:
31+
p2 = p1.next
32+
33+
return newHead

0 commit comments

Comments
 (0)