77. 组合
给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。
你可以按 任何顺序 返回答案。
示例 1:
输入:n = 4, k = 2
输出:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
示例 2:
输入:n = 1, k = 1
输出:[[1]]
提示:
1 <= n <= 20
1 <= k <= n
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
res = []
def dfs(first,cur):
if len(cur) == k:
res.append(cur[:])
return
for i in range(first,n+1):
cur.append(i)
dfs(i+1,cur)
cur.pop()
dfs(1,[])
return res
39. 组合总和
给定一个无重复元素的正整数数组 candidates 和一个正整数 target ,找出 candidates 中所有可以使数字和为目标数 target 的唯一组合。
candidates 中的数字可以无限制重复被选取。如果至少一个所选数字数量不同,则两种组合是唯一的。
对于给定的输入,保证和为 target 的唯一组合数少于 150 个。
示例 1:
输入: candidates = [2,3,6,7], target = 7
输出: [[7],[2,2,3]]
示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
输入: candidates = [2], target = 1
输出: []
示例 4:
输入: candidates = [1], target = 1
输出: [[1]]
示例 5:
输入: candidates = [1], target = 2
输出: [[1,1]]
提示:
1 <= candidates.length <= 30
1 <= candidates[i] <= 200
candidate 中的每个元素都是独一无二的。
1 <= target <= 500
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
res,n = [], len(candidates)
candidates = sorted(candidates)
def dfs(first, curr, resid):
if resid == 0:
res.append(curr[:])
return
for i in range(first,n):
if candidates[i] > resid:
break
curr.append(candidates[i])
dfs(i, curr, resid-candidates[i])
curr.pop()
dfs(0,[],target)
return res
40. 组合总和 II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用一次。
注意:解集不能包含重复的组合。
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]
提示:
1 <= candidates.length <= 100
1 <= candidates[i] <= 50
1 <= target <= 30
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
res,n = [], len(candidates)
candidates = sorted(candidates)
def dfs(first,curr,residue):
if residue == 0:
res.append(curr[:])
return
for i in range(first,n):
if candidates[i] >residue:
break
if i > first and candidates[i-1] == candidates[i]:
continue
curr.append(candidates[i])
dfs(i+1,curr,residue-candidates[i])
curr.pop()
dfs(0,[],target)
return res
全排列(无重复元素)
给定一个不含重复数字的整数数组 nums ,返回其 所有可能的全排列 。可以 按任意顺序 返回答案。
示例 1:
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
示例 2:
输入:nums = [0,1]
输出:[[0,1],[1,0]]
示例 3:
输入:nums = [1]
输出:[[1]]
提示:
1 <= nums.length <= 6
-10 <= nums[i] <= 10
nums 中的所有整数 互不相同
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
res = []
def backtrack(nums,tmp):
if not nums:
res.append(tmp)
return
for i in range(len(nums)):
backtrack(nums[:i]+nums[i+1:],tmp+[nums[i]])
backtrack(nums,[])
return res
全排列II(有重复数字)
给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。
示例 1:
输入:nums = [1,1,2]
输出:
[[1,1,2],
[1,2,1],
[2,1,1]]
示例 2:
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
提示:
1 <= nums.length <= 8
-10 <= nums[i] <= 10
class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
res = []
if not nums:
return nums
def baccktrack(nums,tmp):
if not nums:
res.append(tmp)
return
dic = set()
for i in range(len(nums)):
if nums[i] in dic:
continue
baccktrack(nums[:i]+nums[i+1:], tmp+[nums[i]])
dic.add(nums[i])
baccktrack(nums,[])
return res