mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
update 0040.组合总和II: 修改错字,更换 go 代码
This commit is contained in:
@ -110,13 +110,13 @@ if (sum == target) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
`sum > target` 这个条件其实可以省略,因为和在递归单层遍历的时候,会有剪枝的操作,下面会介绍到。
|
`sum > target` 这个条件其实可以省略,因为在递归单层遍历的时候,会有剪枝的操作,下面会介绍到。
|
||||||
|
|
||||||
* **单层搜索的逻辑**
|
* **单层搜索的逻辑**
|
||||||
|
|
||||||
这里与[39.组合总和](https://programmercarl.com/0039.组合总和.html)最大的不同就是要去重了。
|
这里与[39.组合总和](https://programmercarl.com/0039.组合总和.html)最大的不同就是要去重了。
|
||||||
|
|
||||||
前面我们提到:要去重的是“同一树层上的使用过”,如果判断同一树层上元素(相同的元素)是否使用过了呢。
|
前面我们提到:要去重的是“同一树层上的使用过”,如何判断同一树层上元素(相同的元素)是否使用过了呢。
|
||||||
|
|
||||||
**如果`candidates[i] == candidates[i - 1]` 并且 `used[i - 1] == false`,就说明:前一个树枝,使用了candidates[i - 1],也就是说同一树层使用过candidates[i - 1]**。
|
**如果`candidates[i] == candidates[i - 1]` 并且 `used[i - 1] == false`,就说明:前一个树枝,使用了candidates[i - 1],也就是说同一树层使用过candidates[i - 1]**。
|
||||||
|
|
||||||
@ -438,76 +438,74 @@ class Solution:
|
|||||||
|
|
||||||
**使用used数组**
|
**使用used数组**
|
||||||
```go
|
```go
|
||||||
|
var (
|
||||||
|
res [][]int
|
||||||
|
path []int
|
||||||
|
used []bool
|
||||||
|
)
|
||||||
func combinationSum2(candidates []int, target int) [][]int {
|
func combinationSum2(candidates []int, target int) [][]int {
|
||||||
var trcak []int
|
res, path = make([][]int, 0), make([]int, 0, len(candidates))
|
||||||
var res [][]int
|
used = make([]bool, len(candidates))
|
||||||
var history map[int]bool
|
sort.Ints(candidates) // 排序,为剪枝做准备
|
||||||
history=make(map[int]bool)
|
dfs(candidates, 0, target)
|
||||||
sort.Ints(candidates)
|
|
||||||
backtracking(0,0,target,candidates,trcak,&res,history)
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
func backtracking(startIndex,sum,target int,candidates,trcak []int,res *[][]int,history map[int]bool){
|
|
||||||
//终止条件
|
func dfs(candidates []int, start int, target int) {
|
||||||
if sum==target{
|
if target == 0 { // target 不断减小,如果为0说明达到了目标值
|
||||||
tmp:=make([]int,len(trcak))
|
tmp := make([]int, len(path))
|
||||||
copy(tmp,trcak)//拷贝
|
copy(tmp, path)
|
||||||
*res=append(*res,tmp)//放入结果集
|
res = append(res, tmp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if sum>target{return}
|
for i := start; i < len(candidates); i++ {
|
||||||
//回溯
|
if candidates[i] > target { // 剪枝,提前返回
|
||||||
|
break
|
||||||
|
}
|
||||||
// used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
|
// used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
|
||||||
// used[i - 1] == false,说明同一树层candidates[i - 1]使用过
|
// used[i - 1] == false,说明同一树层candidates[i - 1]使用过
|
||||||
for i:=startIndex;i<len(candidates);i++{
|
if i > 0 && candidates[i] == candidates[i-1] && used[i-1] == false {
|
||||||
if i>0&&candidates[i]==candidates[i-1]&&history[i-1]==false{
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
//更新路径集合和sum
|
path = append(path, candidates[i])
|
||||||
trcak=append(trcak,candidates[i])
|
used[i] = true
|
||||||
sum+=candidates[i]
|
dfs(candidates, i+1, target - candidates[i])
|
||||||
history[i]=true
|
used[i] = false
|
||||||
//递归
|
path = path[:len(path) - 1]
|
||||||
backtracking(i+1,sum,target,candidates,trcak,res,history)
|
|
||||||
//回溯
|
|
||||||
trcak=trcak[:len(trcak)-1]
|
|
||||||
sum-=candidates[i]
|
|
||||||
history[i]=false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
**不使用used数组**
|
**不使用used数组**
|
||||||
```go
|
```go
|
||||||
|
var (
|
||||||
|
res [][]int
|
||||||
|
path []int
|
||||||
|
)
|
||||||
func combinationSum2(candidates []int, target int) [][]int {
|
func combinationSum2(candidates []int, target int) [][]int {
|
||||||
var trcak []int
|
res, path = make([][]int, 0), make([]int, 0, len(candidates))
|
||||||
var res [][]int
|
sort.Ints(candidates) // 排序,为剪枝做准备
|
||||||
sort.Ints(candidates)
|
dfs(candidates, 0, target)
|
||||||
backtracking(0,0,target,candidates,trcak,&res)
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
func backtracking(startIndex,sum,target int,candidates,trcak []int,res *[][]int){
|
|
||||||
//终止条件
|
func dfs(candidates []int, start int, target int) {
|
||||||
if sum==target{
|
if target == 0 { // target 不断减小,如果为0说明达到了目标值
|
||||||
tmp:=make([]int,len(trcak))
|
tmp := make([]int, len(path))
|
||||||
//拷贝
|
copy(tmp, path)
|
||||||
copy(tmp,trcak)
|
res = append(res, tmp)
|
||||||
//放入结果集
|
|
||||||
*res=append(*res,tmp)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//回溯
|
for i := start; i < len(candidates); i++ {
|
||||||
for i:=startIndex;i<len(candidates) && sum+candidates[i]<=target;i++{
|
if candidates[i] > target { // 剪枝,提前返回
|
||||||
// 若当前树层有使用过相同的元素,则跳过
|
break
|
||||||
if i>startIndex&&candidates[i]==candidates[i-1]{
|
}
|
||||||
|
// i != start 限制了这不对深度遍历到达的此值去重
|
||||||
|
if i != start && candidates[i] == candidates[i-1] { // 去重
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
//更新路径集合和sum
|
path = append(path, candidates[i])
|
||||||
trcak=append(trcak,candidates[i])
|
dfs(candidates, i+1, target - candidates[i])
|
||||||
sum+=candidates[i]
|
path = path[:len(path) - 1]
|
||||||
backtracking(i+1,sum,target,candidates,trcak,res)
|
|
||||||
//回溯
|
|
||||||
trcak=trcak[:len(trcak)-1]
|
|
||||||
sum-=candidates[i]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user