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

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

参考Combination Sum II,把去重条件去掉(1-9本身就不包含重复元素),仅返回包含k个元素的vector

class Solution {
public:
    vector<vector<int>> combinationSum3(int k, int n) {
        vector<int> num(9);
        for(int i = 0; i < 9; i ++)
            num[i] = i+1;
        return combinationSum2(num, n, k);
    }
    vector<vector<int> > combinationSum2(vector<int> &num, int target, int k) {
        sort(num.begin(), num.end());
        vector<vector<int> > ret;
        vector<int> cur;
        Helper(ret, cur, num, target, 0, k);
        return ret;
    }
    void Helper(vector<vector<int> > &ret, vector<int> cur, vector<int> &num, int target, int position, int k)
    {
        if(target == 0)
        {
            if(cur.size() == k)
                ret.push_back(cur);
            else
                ;
        }
        else
        {
            for(int i = position; i < num.size() && num[i] <= target; i ++)
            {
                cur.push_back(num[i]);
                Helper(ret, cur, num, target-num[i], i+1, k);
                cur.pop_back();
            }
        }
    }
};

时间: 2024-10-27 06:43:56

【LeetCode】216. Combination Sum III的相关文章

【LeetCode】040. Combination Sum II

题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

【LeetCode】039. Combination Sum

题目: Given a set of candidate numbers (C) (without duplicates) 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

【一天一道LeetCode】#40. Combination Sum II

一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including

【Leetcode】Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 思路:简单的动态规划题目,设f(m, n)为从(0, 0)到达(m

【LeetCode】113. Path Sum II 基于Java和C++的解法及分析

113. Path Sum II Total Accepted: 80509 Total Submissions: 284188 Difficulty: Medium Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8

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】Minimum Path Sum 解题报告

[题目] Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. [思路] 求从左上角到右下角的最小路径值,典型的动态规划

【LeetCode】1. Two Sum 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280 Subject 出处:https://leetcode.com/problems/two-sum/ Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input

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