Compare commits
No commits in common. "641f712a20f48d8da77a9d8bc88c20b67f70b35a" and "9a925bccbb17a4cd78593d1da30ffb1951f8c0ca" have entirely different histories.
641f712a20
...
9a925bccbb
@ -1,63 +0,0 @@
|
|||||||
class Solution:
|
|
||||||
def solve(self, board: List[List[str]]) -> None:
|
|
||||||
"""
|
|
||||||
Do not return anything, modify board in-place instead.
|
|
||||||
"""
|
|
||||||
que = []
|
|
||||||
front = -1
|
|
||||||
tail = -1
|
|
||||||
|
|
||||||
def push_back(que, front, tail, ele):
|
|
||||||
lq = len(que)
|
|
||||||
tail += 1
|
|
||||||
if lq <= tail:
|
|
||||||
que.append(ele)
|
|
||||||
else:
|
|
||||||
que[tail] = ele
|
|
||||||
return tail
|
|
||||||
def pop(que, front, tail):
|
|
||||||
front += 1
|
|
||||||
return (que[front], front)
|
|
||||||
d = [(1,0),(-1,0),(0,1),(0,-1)]
|
|
||||||
height = len(board)
|
|
||||||
width = len(board[0])
|
|
||||||
def is_valid(x, y):
|
|
||||||
if x<0 or y < 0 or x >= height or y >= width:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
is_visited = []
|
|
||||||
for x, l in enumerate(board):
|
|
||||||
is_visited.append([])
|
|
||||||
for y in l:
|
|
||||||
is_visited[x].append(0)
|
|
||||||
if height < 3 or width < 3:
|
|
||||||
return board
|
|
||||||
def is_edge(x, y):
|
|
||||||
if x == 0 or y == 0 or x == height - 1 or y == width - 1:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
for x in range(1,height - 1):
|
|
||||||
for y in range(1,width - 1):
|
|
||||||
if board[x][y] == 'X' or is_visited[x][y] == 1: continue
|
|
||||||
tail = push_back(que,front, tail,(x,y))
|
|
||||||
flag = 0
|
|
||||||
last_front = front
|
|
||||||
while front != tail:
|
|
||||||
(top_ele, front) = pop(que, front, tail)
|
|
||||||
for (cx, cy) in d:
|
|
||||||
nx = cx + top_ele[0]
|
|
||||||
ny = cy + top_ele[1]
|
|
||||||
if is_edge(nx, ny):
|
|
||||||
if board[nx][ny] == 'O':
|
|
||||||
flag = 1
|
|
||||||
continue
|
|
||||||
if board[nx][ny] == 'O' and is_visited[nx][ny]==0:
|
|
||||||
tail = push_back(que,front,tail,(nx,ny))
|
|
||||||
is_visited[nx][ny]=1
|
|
||||||
if flag == 0:
|
|
||||||
for idx in range(last_front + 1, front + 1):
|
|
||||||
xx = que[idx][0]
|
|
||||||
yy = que[idx][1]
|
|
||||||
board[xx][yy]='X'
|
|
||||||
return board
|
|
||||||
|
|
@ -1,71 +0,0 @@
|
|||||||
class TrieNode:
|
|
||||||
def __init__(self, val = ''):
|
|
||||||
self.child = []
|
|
||||||
self.is_word = False
|
|
||||||
self.val = val
|
|
||||||
self.child_map = {}
|
|
||||||
class Trie:
|
|
||||||
def __init__(self):
|
|
||||||
self.root = TrieNode()
|
|
||||||
|
|
||||||
def insert(self, word: str) -> None:
|
|
||||||
parent_node = self.root
|
|
||||||
for idx, letter in enumerate(word):
|
|
||||||
if letter not in parent_node.child_map:
|
|
||||||
new_node = TrieNode(letter)
|
|
||||||
parent_node.child.append(new_node)
|
|
||||||
parent_node.child_map[letter] = len(parent_node.child) - 1
|
|
||||||
parent_node = new_node
|
|
||||||
else:
|
|
||||||
idx = parent_node.child_map[letter]
|
|
||||||
parent_node = parent_node.child[idx]
|
|
||||||
parent_node.is_word = True
|
|
||||||
|
|
||||||
def search(self, word: str) -> bool:
|
|
||||||
def subsearch(node, word) -> bool:
|
|
||||||
parent_node = node
|
|
||||||
if len(word) == 0:
|
|
||||||
return parent_node.is_word
|
|
||||||
ch = word[0]
|
|
||||||
rlt = False
|
|
||||||
if ch == '.':
|
|
||||||
flag = 0
|
|
||||||
for child in parent_node.child_map:
|
|
||||||
idx = parent_node.child_map[child]
|
|
||||||
r = subsearch(parent_node.child[idx], word[1:])
|
|
||||||
if r == True: flag = 1
|
|
||||||
if flag == 1:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
if ch not in parent_node.child_map:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
idx = parent_node.child_map[ch]
|
|
||||||
return subsearch(parent_node.child[idx], word[1:])
|
|
||||||
return subsearch(self.root, word)
|
|
||||||
|
|
||||||
def startsWith(self, prefix: str) -> bool:
|
|
||||||
parent_node = self.root
|
|
||||||
for idx, letter in enumerate(prefix):
|
|
||||||
if letter not in parent_node.child_map:
|
|
||||||
return False
|
|
||||||
idx = parent_node.child_map[letter]
|
|
||||||
parent_node = parent_node.child[idx]
|
|
||||||
return True
|
|
||||||
class WordDictionary:
|
|
||||||
def __init__(self):
|
|
||||||
self.tree = Trie()
|
|
||||||
|
|
||||||
def addWord(self, word: str) -> None:
|
|
||||||
self.tree.insert(word)
|
|
||||||
|
|
||||||
def search(self, word: str) -> bool:
|
|
||||||
return self.tree.search(word)
|
|
||||||
|
|
||||||
|
|
||||||
# Your WordDictionary object will be instantiated and called as such:
|
|
||||||
# obj = WordDictionary()
|
|
||||||
# obj.addWord(word)
|
|
||||||
# param_2 = obj.search(word)
|
|
@ -1,38 +0,0 @@
|
|||||||
class Solution:
|
|
||||||
def minMutation(self, startGene: str, endGene: str, bank: list[str]) -> int:
|
|
||||||
if endGene not in bank: return -1
|
|
||||||
que = []
|
|
||||||
front = -1
|
|
||||||
tail = -1
|
|
||||||
def push_back(que, front, tail, ele):
|
|
||||||
tail += 1
|
|
||||||
if tail >= len(que):
|
|
||||||
que.append(ele)
|
|
||||||
else:
|
|
||||||
que[tail] = ele
|
|
||||||
return tail
|
|
||||||
def pop(que, front ,tail):
|
|
||||||
front += 1
|
|
||||||
return (front , que[front])
|
|
||||||
tail = push_back(que,front,tail,(startGene,0))
|
|
||||||
def diff(str1, str2) -> int:
|
|
||||||
rlt = 0
|
|
||||||
for idx, ch in enumerate(str1):
|
|
||||||
if(str1[idx] != str2[idx]): rlt += 1
|
|
||||||
return rlt
|
|
||||||
rlt = 1e5
|
|
||||||
while front != tail:
|
|
||||||
(front, top_ele) = pop(que, front, tail)
|
|
||||||
step = top_ele[1]
|
|
||||||
string = top_ele[0]
|
|
||||||
if diff(string, endGene) == 0: return step
|
|
||||||
for s in bank:
|
|
||||||
diff_num = diff(s, string)
|
|
||||||
# print(diff_num)
|
|
||||||
if diff_num == 1:
|
|
||||||
tail = push_back(que, front, tail, (s, step + 1))
|
|
||||||
return -1
|
|
||||||
|
|
||||||
sol = Solution()
|
|
||||||
print(sol.minMutation("AACCGGTT","AACCGGTA",["AACCGGTA"]))
|
|
||||||
print(sol.minMutation("AACCGGTT","AAACGGTA",["AACCGGTA","AACCGCTA","AAACGGTA"]))
|
|
@ -1,13 +0,0 @@
|
|||||||
class Solution:
|
|
||||||
def combine(self, n: int, k: int) -> List[List[int]]:
|
|
||||||
rlt = []
|
|
||||||
def comb(l, cur, k):
|
|
||||||
if k == 0:
|
|
||||||
rlt.append(l)
|
|
||||||
return
|
|
||||||
for i in range(cur, n + 1):
|
|
||||||
tmp_l = l.copy()
|
|
||||||
tmp_l.append(i)
|
|
||||||
comb(tmp_l, i + 1, k - 1)
|
|
||||||
comb([], 1, k)
|
|
||||||
return rlt
|
|
Loading…
Reference in New Issue
Block a user