Skip to content

Commit 277ff87

Browse files
author
chris
committed
no message
1 parent b84495c commit 277ff87

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

add-two-numbers.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class Solution(object):
2+
3+
#I like this better and it's faster
4+
#Runtime: O(N)
5+
#Space: O(N)
6+
def addTwoNumbers(self, l1, l2):
7+
8+
#get number from the whole linked list
9+
def getTotal(l):
10+
total = 0
11+
x = 0
12+
while l:
13+
total+=l.val*(10**x)
14+
x+=1
15+
l = l.next
16+
return total
17+
18+
total = getTotal(l1)+getTotal(l2)
19+
20+
#put the number back into linked list
21+
num_string = str(total)[::-1]
22+
pre = ListNode(None)
23+
curr = pre
24+
25+
for n in num_string:
26+
curr.next = ListNode(int(n))
27+
curr = curr.next
28+
29+
return pre.next
30+
31+
32+
#This solution is like adding two numbers in paper
33+
#Runtime: O(N)
34+
#Space: O(N)
35+
def addTwoNumbers(self, l1, l2):
36+
pre = ListNode(None)
37+
curr = pre
38+
39+
#carry is either 0 or 1
40+
#because the max value of l1 and l2 is 18, for example
41+
#3+4=7, 7 has only one digit, so carry is 0, we put 7 in the node
42+
#8+7=15, 15 has two digits, so carry is 1, we put 5 in the node
43+
#we take carry into the next round calculation
44+
carry = 0
45+
46+
while l1 or l2:
47+
total = carry
48+
49+
if l1:
50+
total+=l1.val
51+
l1 = l1.next
52+
if l2:
53+
total+=l2.val
54+
l2 = l2.next
55+
56+
if total>=10:
57+
carry=1
58+
curr.next = ListNode(total%10)
59+
else:
60+
carry = 0
61+
curr.next = ListNode(total)
62+
63+
curr = curr.next
64+
65+
#check if there is carry left behind, for example
66+
#[5]+[5]=[0,1]
67+
#both linked list are done iterate, but still haven't finish adding
68+
if carry!=0:
69+
curr.next = ListNode(carry)
70+
71+
return pre.next

number-of-islands.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
class Solution(object):
2+
3+
"""
4+
we assume 1 means unexplored, 2 is explored
5+
when we discover an unexplored place we count+=1
6+
and use explore_adjacent() to mark the whole island to 2, so we will not count it again
7+
"""
8+
9+
#DFS
10+
def numIslands(self, grid):
11+
if grid is None or grid==[] or grid==[[]]: return 0
12+
13+
count = 0
14+
height = len(grid)
15+
width = len(grid[0])
16+
17+
#explore_adjacent() if grid[h][w] is unexplored, mark it as explored. 1->2.
18+
#and continue to do the same to the adjacent
19+
def explore_adjacent(h, w):
20+
#check border
21+
if h<0 or h>height-1: return
22+
if w<0 or w>width-1: return
23+
24+
#if grid[h][w]==0: it is sea, return
25+
#if grid[h][w]==2: we already explore this place, return
26+
if grid[h][w]=='1':
27+
grid[h][w] = '2'
28+
explore_adjacent(h+1, w)
29+
explore_adjacent(h-1, w)
30+
explore_adjacent(h, w+1)
31+
explore_adjacent(h, w-1)
32+
return
33+
34+
for h in range(height):
35+
for w in range(width):
36+
#if v==0: it is sea, continue to find unexplored
37+
#if v==2: we already explore this place, continue to find unexplored
38+
if grid[h][w]=='1':
39+
#if we discover an unexplored place
40+
#count it
41+
#and set it as root to explore the whole island
42+
count+=1
43+
explore_adjacent(h, w)
44+
return count
45+
46+
#BFS
47+
def numIslands(self, grid):
48+
if grid is None or grid==[] or grid==[[]]: return 0
49+
50+
count = 0
51+
height = len(grid)
52+
width = len(grid[0])
53+
54+
def explore_adjacent(h_root, w_root):
55+
if h_root<0 or h_root>height-1: return
56+
if w_root<0 or w_root>width-1: return
57+
58+
#start the queue from root
59+
queue = [(h_root, w_root)]
60+
61+
while len(queue)>0:
62+
coor = queue.pop(0)
63+
h = coor[0]
64+
w = coor[1]
65+
66+
#check border
67+
if h<0 or h>height-1: continue
68+
if w<0 or w>width-1: continue
69+
70+
if grid[h][w]=='1':
71+
grid[h][w] = '2'
72+
queue.extend([(h+1, w), (h-1, w), (h, w+1), (h, w-1)])
73+
74+
#this function will end if there are no new adjacent to add to queue
75+
#which means the whole island explored (mark as 2)
76+
return
77+
78+
for h in range(height):
79+
for w in range(width):
80+
if grid[h][w]=='1':
81+
#if we discover an unexplored place
82+
#count it
83+
#and set it as root to explore the whole island
84+
count+=1
85+
explore_adjacent(h, w)
86+
87+
return count

0 commit comments

Comments
 (0)