# Definition for a binary tree node. classTreeNode: def__init__(self, x): self.val = x self.left = None self.right = None
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import collections classSolution: defmaxDepth(self, root: TreeNode) -> int: ifnot root: return0 queue = collections.deque() queue.append(root) ans = 0 while queue: ans += 1 for _ in range(len(queue)): node = queue.popleft() if node.left: queue.append(node.left) if node.right: queue.append(node.right) return ans
# Definition for a binary tree node. classTreeNode: def__init__(self, x): self.val = x self.left = None self.right = None
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import collections classSolution: defminDepth(self, root: TreeNode) -> int: ifnot root: return0 ans = 0 queue = collections.deque() queue.append(root) while queue: ans += 1 for _ in range(len(queue)): node = queue.popleft() if node.left isNoneand node.right isNone: return ans if node.left: queue.append(node.left) if node.right: queue.append(node.right)
classSolution: defsolveNQueens(self, n: int) -> List[List[str]]: self.result = [] self.col = set() self.sum = set() self.dif = set() self._dfs(n, 0, []) # 51题: 输出点阵图 return self._gen(n) # 52题: 输出结果数量 # return len(self.result) def_dfs(self, n, row, state): if row >= n: self.result.append(state) return for col in range(n): if col in self.col or row+col in self.sum or row-col in self.dif: continue self.col.add(col) self.sum.add(row + col) self.dif.add(row - col) self._dfs(n, row + 1, state + [col]) self.col.remove(col) self.sum.remove(row + col) self.dif.remove(row - col) def_gen(self, n): result = [] for res in self.result: graph = [] for i in res: graph.append('.'*i + 'Q' + '.'*(n-i-1)) result.append(graph) return result
classSolution: defsolveSudoku(self, board: List[List[str]]) -> None: """ Do not return anything, modify board in-place instead. """ if board isNoneornot len(board): return self.solve(board) defsolve(self, board: List[List[str]]) -> bool: for i in range(len(board)): for j in range(len(board[0])): if board[i][j] == '.': for c in ["1","2","3","4","5","6","7","8","9"]: if self.isValid(board, i, j, c): board[i][j] = c if self.solve(board): returnTrue else: board[i][j] = '.' returnFalse returnTrue defisValid(self, board, row, col, c) -> bool: for i in range(9): if board[row][i] != '.'and board[row][i] == c: returnFalse if board[i][col] != '.'and board[i][col] == c: returnFalse if board[3 * (row//3) + i//3][3 * (col//3) + i%3] != '.'and board[3 * (row//3) + i//3][3 * (col//3) + i%3] == c: returnFalse returnTrue
36. 有效的数独 Valid Sudoku
第一种方法:利用collections的defaultdict数据结构记录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import collections as cl
classSolution: defisValidSudoku(self, board) -> bool: ifnot board ornot len(board): returnFalse self.row, self.col, self.box = cl.defaultdict(set), cl.defaultdict(set), cl.defaultdict(set) for r in range(9): for c in range(9): if board[r][c] != '.': if board[r][c] notin self.row[r] and board[r][c] notin self.col[c] and board[r][c] notin self.box[(r//3, c//3)]: self.row[r].add(board[r][c]) self.col[c].add(board[r][c]) self.box[(r//3, c//3)].add(board[r][c]) else: returnFalse returnTrue
classSolution: defmySqrt(self, x: int) -> int: if x == 1or x == 0: return x l, r = 1, x while l < r: mid = l + (r - l)//2 if mid*mid <= x < (mid+1)*(mid+1): return mid elif mid*mid < x: l = mid else: r = mid