|
| 1 | +/* |
| 2 | + * @lc app=leetcode.cn id=216 lang=golang |
| 3 | + * |
| 4 | + * [216] 组合总和 III |
| 5 | + * |
| 6 | + * https://leetcode.cn/problems/combination-sum-iii/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Medium (71.17%) |
| 10 | + * Likes: 788 |
| 11 | + * Dislikes: 0 |
| 12 | + * Total Accepted: 320.3K |
| 13 | + * Total Submissions: 450.1K |
| 14 | + * Testcase Example: '3\n7' |
| 15 | + * |
| 16 | + * 找出所有相加之和为 n 的 k 个数的组合,且满足下列条件: |
| 17 | + * |
| 18 | + * |
| 19 | + * 只使用数字1到9 |
| 20 | + * 每个数字 最多使用一次 |
| 21 | + * |
| 22 | + * |
| 23 | + * 返回 所有可能的有效组合的列表 。该列表不能包含相同的组合两次,组合可以以任何顺序返回。 |
| 24 | + * |
| 25 | + * |
| 26 | + * |
| 27 | + * 示例 1: |
| 28 | + * |
| 29 | + * |
| 30 | + * 输入: k = 3, n = 7 |
| 31 | + * 输出: [[1,2,4]] |
| 32 | + * 解释: |
| 33 | + * 1 + 2 + 4 = 7 |
| 34 | + * 没有其他符合的组合了。 |
| 35 | + * |
| 36 | + * 示例 2: |
| 37 | + * |
| 38 | + * |
| 39 | + * 输入: k = 3, n = 9 |
| 40 | + * 输出: [[1,2,6], [1,3,5], [2,3,4]] |
| 41 | + * 解释: |
| 42 | + * 1 + 2 + 6 = 9 |
| 43 | + * 1 + 3 + 5 = 9 |
| 44 | + * 2 + 3 + 4 = 9 |
| 45 | + * 没有其他符合的组合了。 |
| 46 | + * |
| 47 | + * 示例 3: |
| 48 | + * |
| 49 | + * |
| 50 | + * 输入: k = 4, n = 1 |
| 51 | + * 输出: [] |
| 52 | + * 解释: 不存在有效的组合。 |
| 53 | + * 在[1,9]范围内使用4个不同的数字,我们可以得到的最小和是1+2+3+4 = 10,因为10 > 1,没有有效的组合。 |
| 54 | + * |
| 55 | + * |
| 56 | + * |
| 57 | + * |
| 58 | + * 提示: |
| 59 | + * |
| 60 | + * |
| 61 | + * 2 <= k <= 9 |
| 62 | + * 1 <= n <= 60 |
| 63 | + * |
| 64 | + * |
| 65 | + */ |
| 66 | +package jzoffer |
| 67 | + |
| 68 | +// @lc code=start |
| 69 | +func combinationSum3(k int, n int) (res [][]int) { |
| 70 | + nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} |
| 71 | + var dfs func(target int, nums, path []int) |
| 72 | + dfs = func(target int, nums, path []int) { |
| 73 | + if len(path) > k { |
| 74 | + return |
| 75 | + } |
| 76 | + if len(path) == k { |
| 77 | + if target == 0 { |
| 78 | + res = append(res, path) |
| 79 | + } |
| 80 | + return |
| 81 | + } |
| 82 | + for i := 0; i < len(nums); i++ { |
| 83 | + if target-nums[i] < 0 { |
| 84 | + break |
| 85 | + } |
| 86 | + path = append(path, nums[i]) |
| 87 | + pick := append([]int{}, path...) |
| 88 | + dfs(target-nums[i], nums[i+1:], pick) |
| 89 | + path = path[:len(path)-1] |
| 90 | + } |
| 91 | + } |
| 92 | + dfs(n, nums, []int{}) |
| 93 | + return |
| 94 | +} |
| 95 | + |
| 96 | +// TODO 二进制枚举 |
| 97 | +// 作者:力扣官方题解 |
| 98 | +// 链接:https://leetcode.cn/problems/combination-sum-iii/ |
| 99 | +// 来源:力扣(LeetCode) |
| 100 | +// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 |
| 101 | + |
| 102 | +func combinationSum3Binary(k int, n int) (ans [][]int) { |
| 103 | + var temp []int |
| 104 | + check := func(mask int) bool { |
| 105 | + temp = nil |
| 106 | + sum := 0 |
| 107 | + for i := 0; i < 9; i++ { |
| 108 | + if 1<<i&mask > 0 { |
| 109 | + temp = append(temp, i+1) |
| 110 | + sum += i + 1 |
| 111 | + } |
| 112 | + } |
| 113 | + return len(temp) == k && sum == n |
| 114 | + } |
| 115 | + |
| 116 | + for mask := 0; mask < 1<<9; mask++ { |
| 117 | + if check(mask) { |
| 118 | + ans = append(ans, append([]int(nil), temp...)) |
| 119 | + } |
| 120 | + } |
| 121 | + return |
| 122 | +} |
| 123 | + |
| 124 | +// @lc code=end |
0 commit comments