Skip to content

Commit e527ebf

Browse files
committed
2019-07-26
1 parent 8a64ba6 commit e527ebf

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

0211.添加与搜索单词-数据结构设计/0211-添加与搜索单词-数据结构设计.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class WordDictionary(object):
2-
2+
33
def __init__(self):
44
"""
55
Initialize your data structure here.
66
"""
77
self.roots = {}
8-
8+
99
def addWord(self, word):
1010
"""
1111
Adds a word into the data structure.
@@ -17,7 +17,7 @@ def addWord(self, word):
1717
for char in word:
1818
node = node.setdefault(char, {})
1919
node["end"] = True
20-
20+
2121
def search(self, word):
2222
"""
2323
Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
@@ -27,7 +27,7 @@ def search(self, word):
2727
self.res = False
2828
self.searchHelper(self.roots, word)
2929
return self.res
30-
30+
3131
def searchHelper(self, dic, word):
3232
if not word:
3333
self.res |= "end" in dic

0212.单词搜索II/0212-单词搜索II.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ def findWords(self, board, words):
5959
for word in words:
6060
tree.insert(word)
6161
words = set(words)
62+
6263
res = set()
6364
def dfs(x0, y0, node, tmpword):
6465
visited.add((x0, y0))
65-
# print tmpword, x0, y0
6666
for k in range(4):
6767
x = x0 + dx[k]
6868
y = y0 + dy[k]
@@ -72,8 +72,8 @@ def dfs(x0, y0, node, tmpword):
7272
dfs(x, y, node[board[x][y]], tmpword + board[x][y])
7373
visited.remove((x,y))
7474

75-
if tmpword in words:
76-
res.add(tmpword)
75+
if tmpword in words: #找到一个单词了
76+
res.add(tmpword) #用集合避免重复
7777

7878
for i in range(m):
7979
for j in range(n):
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution(object):
2+
def rob(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
if len(nums) == 1:
8+
return nums[0]
9+
return max(self.rob2(nums[1:]), self.rob2(nums[:-1]))
10+
11+
def rob2(self, nums):
12+
"""
13+
:type nums: List[int]
14+
:rtype: int
15+
"""
16+
# return 0
17+
if not nums:
18+
return 0
19+
dp = [0 for _ in nums]
20+
dp[0] = nums[0]
21+
for i in range(1, len(nums)):
22+
if i == 1:
23+
dp[i] = max(dp[0], nums[i])
24+
else:
25+
dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])
26+
return dp[-1]

0 commit comments

Comments
 (0)