leetcode 216. Combination Sum III 求和III ---------- java

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.

Example 1:

Input: k = 3, n = 7

Output:

[[1,2,4]]

Example 2:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]

由于限定了1-9所以比较简单,枚举就可以。让答案数组升序排列,然后向里面放入数就可以了。

tips:用数组要比用ArrayList快。

public class Solution {
    public List<List<Integer>> list;
    public List<List<Integer>> combinationSum3(int k, int n) {
        list = new ArrayList();
        if (n > (18 - k + 1) * k / 2 || n < (1 + k) * k / 2 || k > 9){
            return list;
        }
        int[] nums = new int[k];
        for (int i = 1; i <= 10 - k; i++){
            nums[0] = i;
            getSum(nums, n, 0, k - 1);
        }
        return list;
    }
    public void getSum(int[] ans, int n, int pos, int k){
        if (k == 0){
            int sum = 0;
            for (int i = 0; i < ans.length; i++){
                sum += ans[i];
            }
            if (sum == n){
                List<Integer> ll = new ArrayList();
                for (int i : ans){
                    ll.add(i);
                }
                list.add(ll);
            }
            return ;
        }
        for (int i = ans[pos] + 1; i <= 10 - k; i++){
            ans[pos + 1] = i;
            getSum(ans, n, pos + 1, k - 1);
        }
    }

}
时间: 2024-10-27 06:43:52

leetcode 216. Combination Sum III 求和III ---------- java的相关文章

Java for LeetCode 216 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 39 40 216 Combination Sum I II III

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 (includi

LeetCode 216. 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. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n

Leetcode 216. 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. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n

LeetCode 216. Combination Sum III(DFS)

题目 题意:从1-9中选出k个数之和等于n,这个k个数不能有相同的,输出所有可能的k个数字的集合,结果也不能重复 题解:暴搜,从n开始,每次减去1-9中的某个数字,然后继续递归.要注意剪枝,比如1-9中的数字大于n/k的是不可能存在答案中的,如果n 的值小于sum[k]也是不会有答案的.sum[k]表示k个数字最小和的组合.当然k>=10的时候,也是没有答案的. class Solution { public: vector<vector<int>> res; vector&

LeetCode 39 Combination Sum(满足求和等于target的所有组合)

题目链接: https://leetcode.com/problems/combination-sum/?tab=Description Problem: 给定数组并且给定一个target,求出所有满足求和等于target的数字组合 遍历所有的数组中元素,然后对target进行更新,将该元素添加到tempList中,直到remain等于0时达到条件,可以将该tempList添加到list中 注意:每个元素可以使用多次,因此每次的遍历都要从上次的那个下标开始. 当target更新到小于0的时候,返

[LeetCode] 039. Combination Sum (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 039. Combination Sum (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum/ 代码(github):https://github.com/illuz/leetcode 题意: 给出一些正整数集合,以及一个目标数,从集合中选择一

[leetcode] 040. Combination Sum II (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 040. Combination Sum II (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum-ii/ 代码(github):https://github.com/illuz/leetcode 题意: 跟 039 一样(给出一些正整数集合,

[LeetCode: 题解] 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 (including target) will