Skip to content

Commit

Permalink
2019-07-25
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Jul 25, 2019
1 parent 4c81607 commit 8a64ba6
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 24 deletions.
35 changes: 35 additions & 0 deletions 0207.课程表/0207-课程表.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution(object):
def canFinish(self, numCourses, prerequisites):
"""
:type numCourses: int
:type prerequisites: List[List[int]]
:rtype: bool
"""
from collections import deque
if not prerequisites: #没有前置课的要求
return True

indegree = [0 for _ in range(numCourses)]
adj = [set() for _ in range(numCourses)]

for end, start in prerequisites:
indegree[end] += 1
adj[start].add(end)

queue = deque()
for i, x in enumerate(indegree):
if not x: #入度为0的结点入队
queue.append(i)

cnt = 0
while queue:
cur = queue.popleft()
cnt += 1 #当前的cur满足条件

for neighbor in adj[cur]:
indegree[neighbor] -= 1
if not indegree[neighbor]:
queue.append(neighbor)

return cnt == numCourses

8 changes: 1 addition & 7 deletions 0208.实现Trie(前缀树)/0208-实现Trie(前缀树).py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ def __init__(self):
Initialize your data structure here.
"""
self.root = {}


def insert(self, word):
"""
Expand All @@ -16,10 +15,8 @@ def insert(self, word):
node = self.root
for char in word:
node = node.setdefault(char, {})

node["end"] = True


node["end"] = True

def search(self, word):
"""
Expand All @@ -32,9 +29,7 @@ def search(self, word):
if char not in node:
return False
node = node[char]

return "end" in node


def startsWith(self, prefix):
"""
Expand All @@ -47,7 +42,6 @@ def startsWith(self, prefix):
if char not in node:
return False
node = node[char]

return True


Expand Down
32 changes: 15 additions & 17 deletions 0209.长度最小的子数组/0209-长度最小的子数组.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ def minSubArrayLen(self, s, nums):
"""
if not nums:
return 0
res = len(nums) + 1
lo, hi = 0, -1
subsum = 0
while(lo < len(nums)):

if subsum < s and hi + 1 < len(nums):
hi += 1
subsum += nums[hi]
else:
subsum -= nums[lo]
lo += 1

if subsum >= s:
res = min(res, hi - lo + 1)

return res if res != len(nums) + 1 else 0


left, right = 0, 0
interval_sum = nums[0]
res = 1 << 31
while right < len(nums) and left <= right:
# print nums[left:right + 1], interval_sum, left, right
if interval_sum < s:
right += 1 #需要更多的数,向右拓展
if right < len(nums):
interval_sum += nums[right]
else:
res = min(res, right - left + 1)
interval_sum -= nums[left]
left += 1 #满足条件,试着缩小区间
# res = min(res, right - left + 1)
return res if res != 1 <<31 else 0

35 changes: 35 additions & 0 deletions 0210.课程表II/0210-课程表II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution(object):
def findOrder(self, numCourses, prerequisites):
"""
:type numCourses: int
:type prerequisites: List[List[int]]
:rtype: List[int]
"""
if not prerequisites:
return [i for i in range(numCourses)]

indegree = [0 for _ in range(numCourses)]
adj = [set() for _ in range(numCourses)]

for end, start in prerequisites:
indegree[end] += 1
adj[start].add(end)

from collections import deque
queue = deque()

for i, x in enumerate(indegree):
if not x:
queue.append(i)

res = []
while queue:
cur = queue.popleft()
res.append(cur)

for neighbor in adj[cur]:
indegree[neighbor] -= 1
if indegree[neighbor] == 0:
queue.append(neighbor)

return res if len(res) == numCourses else []
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class WordDictionary(object):

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

def addWord(self, word):
"""
Adds a word into the data structure.
:type word: str
:rtype: None
"""
print word
node = self.roots
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.
:type word: str
:rtype: bool
"""
self.res = False
self.searchHelper(self.roots, word)
return self.res

def searchHelper(self, dic, word):
if not word:
self.res |= "end" in dic
return
node = dic
i, char = 0, word[0]
if char == ".":#È«Æ¥Åä
for x in "abcedfghijklmnopqrstuvwxyz":
if x in node:
self.searchHelper(node[x], word[1:])
elif char in node:
self.searchHelper(node[char], word[1:])
# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)
84 changes: 84 additions & 0 deletions 0212.单词搜索II/0212-单词搜索II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
class Trie(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = {}

def insert(self, word):
"""
Inserts a word into the trie.
:type word: str
:rtype: None
"""
node = self.root
for char in word:
node = node.setdefault(char, {})

node["end"] = True

def search(self, word):
"""
Returns if the word is in the trie.
:type word: str
:rtype: bool
"""
node = self.root
for char in word:
if char not in node:
return False
node = node[char]
return "end" in node

def startsWith(self, prefix):
"""
Returns if there is any word in the trie that starts with the given prefix.
:type prefix: str
:rtype: bool
"""
node = self.root
for char in prefix:
if char not in node:
return False
node = node[char]
return True

class Solution(object):
def findWords(self, board, words):
"""
:type board: List[List[str]]
:type words: List[str]
:rtype: List[str]
"""
if not board or not board[0]:
return []
m, n = len(board), len(board[0])
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
tree = Trie()
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]

if 0 <= x < m and 0 <= y < n and board[x][y] in node and (x, y) not in visited:
visited.add((x, y))
dfs(x, y, node[board[x][y]], tmpword + board[x][y])
visited.remove((x,y))

if tmpword in words:
res.add(tmpword)

for i in range(m):
for j in range(n):
if board[i][j] in tree.root:
visited = set((i,j))
dfs(i, j, tree.root[board[i][j]], board[i][j])
return list(res)

0 comments on commit 8a64ba6

Please sign in to comment.