-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
216 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|