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]]
解法
func combine(n int, k int) [][]int {
res:=[][]int{}
candidates:=[]int{}
for i:=1;i<=n;i++{
candidates=append(candidates,i)
}
dfs(candidates,k,0,nil,&res)
return res
}
func dfs(candidates []int, k int, index int, temp []int, res *[][]int){
if len(temp)==k{
copyTemp:=make([]int,len(temp))
copy(copyTemp,temp)
*res=append(*res,copyTemp)
return
}
for i:=index;i<len(candidates);i++{
if i>index&&candidates[i]==candidates[i-1]{
continue;
}
dfs(candidates,k,i+1,append(temp,candidates[i]),res)
}
}
解题思路
77的搜索过程
216.组合总和III
找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。
说明:
所有数字都是正整数。 解集不能包含重复的组合。
示例1
输入: k = 3, n = 7
输出: [[1,2,4]]
示例2
输入: k = 3, n = 9
输出: [[1,2,6], [1,3,5], [2,3,4]]
解法
func combinationSum3(k int, n int) [][]int {
res:=[][]int{}
candidates:=[]int{1,2,3,4,5,6,7,8,9}
dfs(candidates,0,k,n,&res,nil)
return res
}
func dfs(candidates []int,index int,k int,n int,res *[][]int,temp []int){
if n==0&&k==0{
copyTemp:=make([]int,len(temp))
copy(copyTemp,temp)
*res=append(*res,copyTemp)
return
}
for i:=index;i<len(candidates);i++{
if candidates[i]>n{
return
}
dfs(candidates,i+1,k-1,n-candidates[i],res,append(temp,candidates[i]))
}
}
216的搜索过程
17.电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例1
输入: digits = "23"
输出: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
示例2
输入: digits = ""
输出: []
示例3
输入: digits = "2"
输出: ["a","b","c"]
解法
func letterCombinations(digits string) []string {
hashMap:=map[byte]string{'2':"abc",'3':"def",'4':"ghi",'5':"jkl",'6':"mno",'7':"pqrs",'8':"tuv",'9':"wxyz"}
res:=[]string{}
if len(digits)==0{
return res
}
dfs(digits,0,"",&res,hashMap)
return res
}
func dfs(digits string, index int, temp string, res *[]string,hashMap map[byte]string){
if index==len(digits){
*res=append(*res,temp)
return
}
curStr:=hashMap[digits[index]]
for i:=0;i<len(curStr);i++{
dfs(digits,index+1,temp+string(curStr[i]),res,hashMap)
}
}
解题思路
17的搜索过程
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
输出: []
解法
func combinationSum(candidates []int, target int) [][]int {
res:=[][]int{}
sort.Ints(candidates)
dfs(candidates,&res,0,nil,target)
return res
}
func dfs(candidates []int, res *[][]int,index int, temp []int, target int){
if target==0{
copyTemp:=make([]int,len(temp))
copy(copyTemp,temp)
*res=append(*res,copyTemp)
return
}
for i:=index;i<len(candidates);i++{
if target < candidates[i] {
return
}
dfs(candidates,res,i,append(temp,candidates[i]),target-candidates[i])
}
}
解题思路
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]
]
解法
func combinationSum2(candidates []int, target int) [][]int {
res:=[][]int{}
sort.Ints(candidates)
dfs(candidates,0,target,&res,nil)
return res
}
func dfs(candidates []int, index int, target int, res *[][]int, temp []int){
if target==0{
copyTemp:=make([]int,len(temp))
copy(copyTemp,temp)
*res=append(*res,copyTemp)
return
}
for i:=index;i<len(candidates);i++{
if i>index&&candidates[i]==candidates[i-1]{
continue
}
if candidates[i]>target{
return
}
dfs(candidates,i+1,target-candidates[i],res,append(temp,candidates[i]))
}
}