Skip to content

Commit 4688dc3

Browse files
committed
2019-09-06
1 parent afcc75b commit 4688dc3

File tree

4 files changed

+88
-46
lines changed

4 files changed

+88
-46
lines changed

0286.墙与门/0286-墙与门.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution(object):
2+
def wallsAndGates(self, rooms):
3+
"""
4+
:type rooms: List[List[int]]
5+
:rtype: None Do not return anything, modify rooms in-place instead.
6+
"""
7+
from collections import deque
8+
if not rooms or not rooms[0]:
9+
return rooms
10+
m, n = len(rooms), len(rooms[0])
11+
dx = [1, -1, 0, 0]
12+
dy = [0, 0, 1, -1]
13+
INF = 2147483647
14+
queue = deque()
15+
def bfs(queue):
16+
while queue:
17+
pos = queue.popleft()
18+
x0, y0 = pos
19+
20+
for k in range(4):
21+
x = x0 + dx[k]
22+
y = y0 + dy[k]
23+
if 0 <= x < m and 0 <= y < n and rooms[x][y] == INF:
24+
rooms[x][y] = rooms[x0][y0] + 1
25+
queue.append((x, y))
26+
27+
for i in range(m):
28+
for j in range(n):
29+
if rooms[i][j] == 0: #ÏÖÔÚ´ÓÿÉÈÃųö·¢
30+
queue.append((i, j))
31+
bfs(queue)
32+
return rooms

0287.寻找重复数/0287-寻找重复数.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ def findDuplicate(self, nums):
44
:type nums: List[int]
55
:rtype: int
66
"""
7-
87
slow, fast = 0, 0
9-
while(1):
8+
while 1:
109
slow = nums[slow]
1110
fast = nums[nums[fast]]
12-
# print slow, fast
13-
if slow == fast: # loop exists
11+
if slow == fast:
1412
fast = 0
15-
while(nums[slow] != nums[fast]):
13+
while nums[slow] != nums[fast]:
1614
slow = nums[slow]
1715
fast = nums[fast]
18-
# print slow, fast
19-
return nums[fast]
20-
16+
return nums[fast]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class ValidWordAbbr(object):
2+
3+
def __init__(self, dictionary):
4+
"""
5+
:type dictionary: List[str]
6+
"""
7+
from collections import defaultdict
8+
self.dic = defaultdict(list)
9+
for word in dictionary:
10+
if len(word) <= 2:
11+
self.dic[word].append(word)
12+
else:
13+
self.dic[word[0] + str(len(word) - 2) + word[-1]].append(word)
14+
15+
def isUnique(self, word):
16+
"""
17+
:type word: str
18+
:rtype: bool
19+
"""
20+
if len(word) <= 2:
21+
return not self.dic[word] or self.dic[word] == [word] * len(self.dic[word])
22+
else:
23+
s = word[0] + str(len(word) - 2) + word[-1]
24+
return not self.dic[s] or self.dic[s] == [word] * len(self.dic[s])
25+
26+
27+
28+
# Your ValidWordAbbr object will be instantiated and called as such:
29+
# obj = ValidWordAbbr(dictionary)
30+
# param_1 = obj.isUnique(word)

0289.生命游戏/0289-生命游戏.py

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,36 @@ class Solution(object):
22
def gameOfLife(self, board):
33
"""
44
:type board: List[List[int]]
5-
:rtype: void Do not return anything, modify board in-place instead.
5+
:rtype: None Do not return anything, modify board in-place instead.
66
"""
7-
m = len(board)
8-
if m == 0:
9-
return board
10-
n = len(board[0])
11-
if n == 0:
12-
return board
7+
if not board or not board[0]:
8+
return
9+
m, n = len(board), len(board[0])
10+
dx = [1, -1, 0, 0, 1, 1, -1, -1]
11+
dy = [0, 0, 1, -1, 1, -1, 1, -1]
1312

14-
alivex = list()
15-
alivey = list()
16-
17-
def neibor(x, y): #统计八个邻居里有几个是活细胞(1)
18-
dx = [1, -1, 0, 0, 1, -1, -1, 1]
19-
dy = [0, 0, 1, -1, 1, -1, 1, -1]
20-
13+
def countLiveCells(x0, y0):
2114
cnt = 0
2215
for k in range(8):
23-
xx = x + dx[k]
24-
yy = y + dy[k]
16+
x = x0 + dx[k]
17+
y = y0 + dy[k]
18+
19+
if 0 <= x < m and 0 <= y < n and board[x][y] == 1:
20+
cnt += 1
2521

26-
if 0 <= xx < m and 0 <= yy < n and board[xx][yy] == 1:
27-
cnt += 1
28-
2922
return cnt
3023

31-
24+
liveCellSet = set()
3225
for i in range(m):
3326
for j in range(n):
34-
cnt = neibor(i, j)
35-
# print i, j, cnt
36-
if (board[i][j] == 1 and 2 <= cnt <= 3) or (board[i][j] == 0 and cnt == 3):
37-
alivex.append(i)
38-
alivey.append(j)
39-
40-
alivecnt = 0
27+
if board[i][j] == 1 and countLiveCells(i, j) in [2, 3]:
28+
liveCellSet.add((i, j))
29+
elif board[i][j] == 0 and countLiveCells(i, j) == 3:
30+
liveCellSet.add((i, j))
31+
4132
for i in range(m):
4233
for j in range(n):
43-
board[i][j] = 0
44-
45-
if alivecnt < len(alivex):
46-
if alivex[alivecnt] == i and alivey[alivecnt] == j:
47-
board[i][j] = 1
48-
alivecnt += 1
49-
50-
return board
51-
52-
53-
34+
if (i, j) in liveCellSet:
35+
board[i][j] = 1
36+
else:
37+
board[i][j] = 0

0 commit comments

Comments
 (0)