【LeetCode-面试算法经典-Java实现】【216-Combination Sum III (组合数的和)】

【216-Combination Sum III (组合数的和)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】


代码下载【https://github.com/Wang-Jun-Chao】

原题

  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:

  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]]

题目大意

  寻找所有满足k个数之和等于n的组合,只允许使用数字1-9,并且每一种组合中的数字应该是唯一的。确保组合中的数字以递增顺序排列。

解题思路

  回溯法

代码实现

算法实现类

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Solution {

    public List<List<Integer>> combinationSum3(int k, int n) {
        // 用于保存所有结果
        List<List<Integer>> result = new LinkedList<>();
        // 用于保存中间结果
        List<Integer> list = new LinkedList<>();
        // 条件满足就进行解题操作
        if (k > 0 && k <= 9) {
                solve(k, 1, n, 0, list, result);
        }

        // 返回结果
        return result;
    }

    /**
     * 求解方法
     *
     * @param k         每个解的元素个数
     * @param cur       当前处理第k个元素
     * @param remainder k - cur + 1个元素的和
     * @param prevVal   第cur-1个元素的取值
     * @param list      将解的元素的集合类
     * @param result    保存所有结果的容器
     */
    public void solve(int k, int cur, int remainder, int prevVal, List<Integer> list, List<List<Integer>> result) {
        // 处理最后一个元素
        if (cur == k) {
            // remainder为最后一个解元素的值,它必须大于前一个解元素的值,并且不能超出9
            if (remainder > prevVal && remainder <= 9) {
                // 添加元素值
                list.add(remainder);

                // 拷贝结果到新的队列中
                List<Integer> one = new LinkedList<>();
                for (Integer i : list) {
                    one.add(i);
                }

                // 保存结果
                result.add(one);
                // 删除最后一个元素,进行现场还原
                list.remove(list.size() - 1);
            }
        }
        // 不是最后一个元素
        else if (cur < k){
            for (int i = prevVal + 1; i <= 9 - (k - cur); i++) {
                // 添加元素
                list.add(i);
                // 递归求解
                solve(k, cur + 1, remainder - i, i, list, result);
                // 现场还原
                list.remove(list.size() - 1);

            }
        }
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/48046271

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-14 18:33:47

【LeetCode-面试算法经典-Java实现】【216-Combination Sum III (组合数的和)】的相关文章

【LeetCode-面试算法经典-Java实现】【077-Combinations(组合数)】

[077-Combinations(组合数)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given two integers n and k, return all possible combinations of k numbers out of 1 - n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 题目大意 给

【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题解(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

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

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

216. Combination Sum III java solutions

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&

[leedcode 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