classSolution(object): defgenerateParenthesis(self, n): """ :type n: int :rtype: List[str] """ res = [] defhelper(left_p,right_p,tmp): if left_p == n and right_p == n: res.append(tmp) return if left_p > n or right_p > n or left_p < right_p: return helper(left_p+1,right_p,tmp+'(') helper(left_p,right_p+1,tmp+')') helper(0,0,"") return res
classSolution(object): defsolveSudoku(self, board): """ :type board: List[List[str]] :rtype: None Do not return anything, modify board in-place instead. """ self.backtrack(board) defbacktrack(self,board): for i inrange(9): for j inrange(9): if board[i][j] == '.': for c in'123456789': if self.isPointValid(board,i,j,c): board[i][j] = c if self.backtrack(board): returnTrue else: board[i][j] = '.' returnFalse returnTrue defisPointValid(self,board,x,y,c): for i inrange(9): if board[i][y] == c: returnFalse if board[x][i] == c: returnFalse if board[(x//3)*3+i//3][(y//3)*3+i%3] == c: returnFalse returnTrue
classSolution(object): defcombinationSum(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] """ candidates.sort() n = len(candidates) res = [] defhelper(i,tmp_sum,tmp_list): if i == n or tmp_sum > target: return if tmp_sum == target: res.append(tmp_list) return for j inrange(i,n): if tmp_sum + candidates[j] > target: break helper(j,tmp_sum + candidates[j],tmp_list + [candidates[j]]) helper(0,0,[]) return res
classSolution(object): defpermuteUnique(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ nums.sort() res = [] n = len(nums) visited = set() defhelper(nums,tmp): iflen(tmp) == n: res.append(tmp) return for i inrange(len(nums)): if i in visited or (i>0and i-1notin visited and nums[i] == nums[i-1]): continue visited.add(i) helper(nums,tmp+[nums[i]]) visited.remove(i) helper(nums,[]) return res
当然也有别的回溯的写法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution(object): defpermuteUnique(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ iflen(nums) == 0: return [] iflen(nums) == 1: return [nums] res = [] for i inrange(len(nums)): prefix = nums[i] rest = nums[:i]+nums[i+1:] for j in self.permuteUnique(rest): if [prefix] +j notin res: res.append([prefix] + j) return res
8. N皇后
n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
上图为 8 皇后问题的一种解法。
给定一个整数 n,返回所有不同的 n 皇后问题的解决方案。
每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。
classSolution(object): defsolveNQueens(self, n): """ :type n: int :rtype: List[List[str]] """ res = [] s = '.' * n defhelper(i,tmp,col,z_dia,f_dia): if i == n: res.append(tmp) return for j inrange(n): if j notin col and (i+j) notin z_dia and (i-j) notin f_dia: helper(i+1,tmp+[s[:j]+'Q'+s[j+1:]],col | {j},z_dia | {i+j},f_dia | {i-j}) helper(0,[],set(),set(),set()) return res
classSolution(object): deftotalNQueens(self, n): """ :type n: int :rtype: int """ self.res = 0 defhelper(i,col,z_dia,f_dia): if i == n: returnTrue for j inrange(n): if j notin col and (i+j) notin z_dia and (i-j) notin f_dia: if helper(i+1,col | {j},z_dia | {i+j},f_dia | {i-j}): self.res += 1 returnFalse helper(0,set(),set(),set()) return self.res