LeetCode 216. 组合总和 III(Combination Sum III)

题目描述

找出所有相加之和为 的 个数的组合组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。

说明:

  • 所有数字都是正整数。
  • 解集不能包含重复的组合。

示例 1:

输入: k = 3, n = 7
输出: [[1,2,4]]

示例 2:

输入: k = 3, n = 9
输出: [[1,2,6], [1,3,5], [2,3,4]]

解题思路

回溯算法,从第一个数开始依次添加数字并比较当前数字总和,若相等就添加到结果集合中。

代码

 1 class Solution {
 2 public:
 3     vector<vector<int>> combinationSum3(int k, int n) {
 4         vector<vector<int>> res;
 5         if(k <= 0 || n <= 0) return res;
 6         vector<int> nums, temp;
 7         for(int i = 0; i < 9; i++)
 8             nums.push_back(i + 1);
 9         combine(nums, 0, 0, k, n, temp, res);
10         return res;
11     }
12     void combine(vector<int> nums, int idx, int sum, int k, int n, vector<int> &temp, vector<vector<int>> &res){
13         if(k == 0 && sum == n)
14             res.push_back(temp);
15         else if(sum < n){
16             for(int i = idx; i < 9; i++)
17                 if(sum + nums[i] <= n){
18                     temp.push_back(nums[i]);
19                     combine(nums, i + 1, sum + nums[i], k - 1, n, temp, res);
20                     temp.pop_back();
21                 }
22         }
23     }
24 };

原文地址:https://www.cnblogs.com/wmx24/p/9529005.html

时间: 2024-11-09 08:47:01

LeetCode 216. 组合总和 III(Combination Sum III)的相关文章

LeetCode 39. 组合总和(Combination Sum)

题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选取. 说明: 所有数字(包括 target)都是正整数. 解集不能包含重复的组合. 示例 1: 输入: candidates = [2,3,6,7], target = 7, 所求解集为: [ [7], [2,2,3] ] 示例 2: 输入: candidates = [2,3,5], t

leetcode 216. 组合总和 III

找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有1 - 9的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的组合. 示例 1: 输入: k = 3, n = 7 输出: [[1,2,4]] 示例 2: 输入: k = 3, n = 9 输出: [[1,2,6], [1,3,5], [2,3,4]] 思路:和上一题的思路一样, 只是加了一个条件, 长度要是规定的长度 1 class Solution { 2 public: 3 void dfs

【LeetCode】216. Combination Sum III

Combination Sum III Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascend

[LeetCode] Combination Sum III 组合之和之三

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

[LeetCode][JavaScript]Combination Sum III

Combination Sum III Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascend

Leetcode题解(4):L216/Combination Sum III

L216: Combination Sum III Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in

LeetCode Combination Sum III

原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 与Combination Sum II相似,不同的是中不是所有元素相加,只是k个元素相加. 所以在把item的copy 加到res前需要同时满足item.size() == k 和 target == 0两个条件. AC Java: 1 public class Solution { 2 public List<List<Integer>> combinationS

Combination Sum,Combination Sum II,Combination Sum III

39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (inc

[Leetcode 40]组合数和II Combination Sum II

[题目] Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: A