代码随想录算法训练营第27天|● 39. 组合总和 ● 40.组合总和II ● 131.分割回文串
39. 组合总和
已解答
中等
相关标签
相关企业
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
示例 1:
输入:candidates = [2,3,6,7], target = 7输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
输入: candidates = [2], target = 1
输出: []
提示:
- 1 <= candidates.length <= 30
- 2 <= candidates[i] <= 40
- candidates 的所有元素 互不相同
- 1 <= target <= 40
思路
- 两个点没注意到
- 循环终止条件:sum>target
- 需要index
代码
func combinationSum(candidates []int, target int) [][]int {res := make([][]int, 0)dfs_cS(candidates, 0, target, 0, []int{}, &res)return res
}func dfs_cS(candidates []int, startindex int, target int, sum int, comb []int, res *[][]int) {if sum > target {//***return}if sum == target {tmp := make([]int, len(comb))copy(tmp, comb)*res = append(*res, tmp)return}for i := startindex; i < len(candidates); i++ {//***sum += candidates[i]comb = append(comb, candidates[i])dfs_cS(candidates, i, target, sum, comb, res)//i不移动,可以重复选取数值sum -= candidates[i]comb = comb[:len(comb)-1]}return
}
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
思路
这道题目和39.组合总和 (opens new window)如下区别:
- 本题candidates 中的每个数字在每个组合中只能使用一次。
- 本题数组candidates的元素是有重复的,而39.组合总和 (opens new window)是无重复元素的数组candidates
最后本题和39.组合总和 (opens new window)要求一样,解集不能包含重复的组合。
本题的难点在于区别2中:集合(数组candidates)有重复元素,但还不能有重复的组合。
我们是要同一树层上使用过,还是同一树枝上使用过呢?
回看一下题目,元素在同一个组合内是可以重复的,怎么重复都没事,但两个组合不能相同。
所以我们要去重的是同一树层上的“使用过”,同一树枝上的都是一个组合里的元素,不用去重。
为了理解去重我们来举一个例子,candidates = [1, 1, 2], target = 3,(方便起见candidates已经排序了)
强调一下,树层去重的话,需要对数组排序!
选择过程树形结构如图所示:
[图片]
代码
func combinationSum2(candidates []int, target int) [][]int {res := make([][]int, 0)sort.Ints(candidates)dfs_cS2(candidates, 0, 0, target, []int{}, &res)return res
}func dfs_cS2(candidates []int, index int, sum int, target int, comb []int, res *[][]int) {if sum > target {return}if sum == target {tmp := make([]int, len(comb))copy(tmp, comb)*res = append(*res, tmp)return}for i := index; i < len(candidates); i++ {// i != start 限制了这不对深度遍历到达的此值去重if i != index && candidates[i] == candidates[i-1]{ //去重continue}sum += candidates[i]comb = append(comb, candidates[i])dfs_cS2(candidates, i+1, sum, target, comb, res)sum -= candidates[i]comb = comb[:len(comb)-1]}return
}
131. 分割回文串
中等
相关标签
相关企业
给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。
示例 1:
输入:s = “aab”
输出:[[“a”,“a”,“b”],[“aa”,“b”]]
示例 2:
输入:s = “a”
输出:[[“a”]]
提示:
- 1 <= s.length <= 16
- s 仅由小写英文字母组成
代码
func partition(s string) [][]string {res := make([][]string, 0)dfs_p(s, 0, []string{}, &res)return res
}func dfs_p(s string, index int, path []string, res *[][]string) {if index >= len(s) {tmp := make([]string, len(path))copy(tmp, path)*res = append(*res, tmp)return}for i := index; i < len(s); i++ {if isRoll(s, index, i) {path = append(path, s[index:i+1])} else {continue}dfs_p(s, i+1, path, res)path = path[:len(path)-1]}return
}
func isRoll(s string, i int, j int) bool {for i < j {if s[i] == s[j] {i++j--} else {return false}}return true
}