目录
注意:
17. 电话号码的字母组合
79. 单词搜索
46. 全排列
47. 全排列 II
78. 子集
90. 子集 II
216. 组合总和 III
52. N皇后 II
473. 火柴拼正方形
继续和b站宝藏up主大雪菜一起刷题
https://www.bilibili.com/video/BV1M4411Q7td
参考刷题视频
思路:
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if not digits:
return []
ch = ["abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]
res = [""]
for i in range(len(digits)):
now = digits[i]
tmp = []
for c in ch[ord(now)-ord('2')]:
for j in res:
tmp.append(j+c)
res = tmp
return res
思路:
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
if len(board)==0 or len(board[0])==0:
return False
for i in range(len(board)):
for j in range(len(board[0])):
if self.dfs(board, word, i, j, 0):
return True
return False
def dfs(self, board, word, x, y, u):
move = [(0,1),(1,0),(0,-1),(-1,0)]
if board[x][y]!=word[u]:
return False
if u == len(word)-1:
return True
board[x][y] = '.'
for i in range(4):
newx = x + move[i][0]
newy = y + move[i][1]
if 0<=newx
思路:
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
visited = [False]*len(nums)
ans = []
self.dfs(nums, 0, [], ans, visited)
return ans
def dfs(self, nums, u, path, ans, visited):
if u == len(nums):
ans.append(path[:])
return
for j in range(len(nums)):
if not visited[j]:
visited[j] = True
path.append(nums[j])
self.dfs(nums, u+1, path, ans, visited)
path.pop()
visited[j] = False
思路:
class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
nums = sorted(nums)
ans = []
path = [-1]*len(nums)
visited = [False]*len(nums)
self.dfs(nums, 0, path, ans, 0, visited)
return ans
def dfs(self, nums, u, path, ans, start, visited):
if u == len(nums):
ans.append(path[:])
return
for i in range(start, len(nums)):
if not visited[i]:
visited[i] = True
path[i] = nums[u]
if u+1
思路:
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
res = []
for i in range(2**len(nums)):
tmp = []
for j in range(len(nums)):
# print(i<>j)&1:
tmp.append(nums[j])
res.append(tmp)
return res
思路:
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
res = []
nums = sorted(nums)
self.dfs(nums, 0, res, [])
return res
def dfs(self, nums, u, res, path):
if u == len(nums):
res.append(path[:])
return
# 统计有几个nums[u]
k = 0
while k+u < len(nums) and nums[k+u]==nums[u]:
k += 1
for i in range(k+1):
self.dfs(nums, u+k, res, path)
path.append(nums[u])
# 恢复现场
for i in range(k+1):
path.pop()
思路:
class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
res = []
self.dfs(k, 1, n, res, [])
return res
def dfs(self, k, start, n, res, path):
if not k:
if not n:
res.append(path[:])
return
for i in range(start, 10):
path.append(i)
self.dfs(k-1, i+1, n-i, res, path)
path.pop()
思路:
class Solution:
def totalNQueens(self, n: int) -> int:
res = [0]
col = [False]*(n)
d = [False]*(2*n)
ud = [False]*(2*n)
self.dfs(0, res, n, col, d, ud)
return res[0]
def dfs(self, u, res, n, col, d, ud):
if u == n:
print(res)
res[0] += 1
return
for i in range(n):
if (not col[i]) and (not d[u+i]) and (not ud[u-i+n]):
# print("{} {}".format(i, n))
col[i] = d[u+i] = ud[u-i+n] = True
self.dfs(u+1, res, n, col, d, ud)
col[i] = d[u+i] = ud[u-i+n] = False
思路:
class Solution:
def makesquare(self, nums: List[int]) -> bool:
# 查看过与否的标记
visited = [False]*len(nums)
# 列表之和
sum_ = sum(nums)
if (sum_ == 0) or (sum_%4 != 0):
return False
# 排序 使一样的数字挤在一起
nums = sorted(nums, reverse = True)
return self.dfs(nums, visited, 0, 0, sum_//4)
def dfs(self, nums, visited, u, cur, length):
# 得到一个满足条件的边
if cur == length:
u += 1
cur = 0
# 四条边都得到了
if u == 4:
return True
i = 0
while i < len(nums):
if not visited[i] and nums[i]<=length-cur:
visited[i] = True
if self.dfs(nums, visited, u, cur+nums[i], length):
return True
visited[i] = False
# 剪枝4
if not cur:
return False
# 剪枝5
if cur+nums[i] == length:
return False
# 剪枝3
if i+1