Skip to content

Commit

Permalink
2019-07-26
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Jul 26, 2019
1 parent 8a64ba6 commit e527ebf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class WordDictionary(object):

def __init__(self):
"""
Initialize your data structure here.
"""
self.roots = {}

def addWord(self, word):
"""
Adds a word into the data structure.
Expand All @@ -17,7 +17,7 @@ def addWord(self, word):
for char in word:
node = node.setdefault(char, {})
node["end"] = True

def search(self, word):
"""
Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
Expand All @@ -27,7 +27,7 @@ def search(self, word):
self.res = False
self.searchHelper(self.roots, word)
return self.res

def searchHelper(self, dic, word):
if not word:
self.res |= "end" in dic
Expand Down
6 changes: 3 additions & 3 deletions 0212.单词搜索II/0212-单词搜索II.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def findWords(self, board, words):
for word in words:
tree.insert(word)
words = set(words)

res = set()
def dfs(x0, y0, node, tmpword):
visited.add((x0, y0))
# print tmpword, x0, y0
for k in range(4):
x = x0 + dx[k]
y = y0 + dy[k]
Expand All @@ -72,8 +72,8 @@ def dfs(x0, y0, node, tmpword):
dfs(x, y, node[board[x][y]], tmpword + board[x][y])
visited.remove((x,y))

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

for i in range(m):
for j in range(n):
Expand Down
26 changes: 26 additions & 0 deletions 0213.打家劫舍II/0213-打家劫舍II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 1:
return nums[0]
return max(self.rob2(nums[1:]), self.rob2(nums[:-1]))

def rob2(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# return 0
if not nums:
return 0
dp = [0 for _ in nums]
dp[0] = nums[0]
for i in range(1, len(nums)):
if i == 1:
dp[i] = max(dp[0], nums[i])
else:
dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])
return dp[-1]

0 comments on commit e527ebf

Please sign in to comment.