pass 211 77 433
This commit is contained in:
parent
75690d88f7
commit
641f712a20
63
130-240602-pass/main.py
Normal file
63
130-240602-pass/main.py
Normal file
@ -0,0 +1,63 @@
|
||||
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
|
||||
|
71
211-240602-pass/main.py
Normal file
71
211-240602-pass/main.py
Normal file
@ -0,0 +1,71 @@
|
||||
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)
|
13
77-240602-pass/main.py
Normal file
13
77-240602-pass/main.py
Normal file
@ -0,0 +1,13 @@
|
||||
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