mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
规范格式
This commit is contained in:
29
leetcode/0039.Combination-Sum/39. Combination Sum.go
Normal file
29
leetcode/0039.Combination-Sum/39. Combination Sum.go
Normal file
@ -0,0 +1,29 @@
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
func combinationSum(candidates []int, target int) [][]int {
|
||||
if len(candidates) == 0 {
|
||||
return [][]int{}
|
||||
}
|
||||
c, res := []int{}, [][]int{}
|
||||
sort.Ints(candidates)
|
||||
findcombinationSum(candidates, target, 0, c, &res)
|
||||
return res
|
||||
}
|
||||
|
||||
func findcombinationSum(nums []int, target, index int, c []int, res *[][]int) {
|
||||
if target <= 0 {
|
||||
if target == 0 {
|
||||
b := make([]int, len(c))
|
||||
copy(b, c)
|
||||
*res = append(*res, b)
|
||||
}
|
||||
return
|
||||
}
|
||||
for i := index; i < len(nums); i++ {
|
||||
c = append(c, nums[i])
|
||||
findcombinationSum(nums, target-nums[i], i, c, res) // 注意这里迭代的时候 index 依旧不变,因为一个元素可以取多次
|
||||
c = c[:len(c)-1]
|
||||
}
|
||||
}
|
48
leetcode/0039.Combination-Sum/39. Combination Sum_test.go
Normal file
48
leetcode/0039.Combination-Sum/39. Combination Sum_test.go
Normal file
@ -0,0 +1,48 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question39 struct {
|
||||
para39
|
||||
ans39
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para39 struct {
|
||||
n []int
|
||||
k int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans39 struct {
|
||||
one [][]int
|
||||
}
|
||||
|
||||
func Test_Problem39(t *testing.T) {
|
||||
|
||||
qs := []question39{
|
||||
|
||||
question39{
|
||||
para39{[]int{2, 3, 6, 7}, 7},
|
||||
ans39{[][]int{[]int{7}, []int{2, 2, 3}}},
|
||||
},
|
||||
|
||||
question39{
|
||||
para39{[]int{2, 3, 5}, 8},
|
||||
ans39{[][]int{[]int{2, 2, 2, 2}, []int{2, 3, 3}, []int{3, 5}}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 39------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans39, q.para39
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, combinationSum(p.n, p.k))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
48
leetcode/0039.Combination-Sum/README.md
Executable file
48
leetcode/0039.Combination-Sum/README.md
Executable file
@ -0,0 +1,48 @@
|
||||
# [39. Combination Sum](https://leetcode.com/problems/combination-sum/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a **set** of candidate numbers (`candidates`) **(without duplicates)** and a target number (`target`), find all unique combinations in `candidates` where the candidate numbers sums to `target`.
|
||||
|
||||
The **same** repeated number may be chosen from `candidates` unlimited number of times.
|
||||
|
||||
**Note:**
|
||||
|
||||
- All numbers (including `target`) will be positive integers.
|
||||
- The solution set must not contain duplicate combinations.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
|
||||
Input: candidates = [2,3,6,7], target = 7,
|
||||
A solution set is:
|
||||
[
|
||||
[7],
|
||||
[2,2,3]
|
||||
]
|
||||
|
||||
|
||||
**Example 2:**
|
||||
|
||||
|
||||
Input: candidates = [2,3,5], target = 8,
|
||||
A solution set is:
|
||||
[
|
||||
[2,2,2,2],
|
||||
[2,3,3],
|
||||
[3,5]
|
||||
]
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
|
||||
|
||||
candidates 中的数字可以无限制重复被选取。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 题目要求出总和为 sum 的所有组合,组合需要去重。
|
||||
- 这一题和第 47 题类似,只不过元素可以反复使用。
|
Reference in New Issue
Block a user