Skip to content

Commit e44e59f

Browse files
committed
feature: add go solution for leetcode 0040
1 parent fddce3c commit e44e59f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func combinationSum2(candidates []int, target int) [][]int {
2+
//排序,然后递归搜索
3+
sort.Ints(candidates)
4+
return dfs(candidates,target)
5+
}
6+
7+
func dfs(nums []int, target int) [][]int {
8+
ret := [][]int{}
9+
for i, n := range nums {
10+
//跳过与之前迭代相同的值
11+
if i > 0 && nums[i-1] == n {
12+
continue
13+
} else if target < n {
14+
break
15+
} else if target == n {
16+
ret = append(ret, []int{n})
17+
continue
18+
}
19+
//不能使用同一位置数字
20+
for _,v := range dfs(nums[i+1:], target - n) {
21+
ret = append(ret,append([]int{n}, v...))
22+
}
23+
}
24+
25+
return ret
26+
}

0 commit comments

Comments
 (0)