【Lintcode】135.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.

Example

Given candidate set [2,3,6,7] and target 7, a solution set is:

[7]
[2, 2, 3]

题解:

  这个题和LeetCode不一样,这个允许重复数字产生,那么输入[2,2,3],7 结果为[2, 2, 3], [2, 2, 3], [2, 2, 3];要么对最后结果去除重复数组,要么在处理之前就对candidates数组去重复,或者利用set不重复的特点添加数组。

Solution 1 ()

class Solution {
public:
    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
        if (candidates.empty()) {
            return {{}};
           }
           set<vector<int> > res;
           vector<int> cur;
           sort(candidates.begin(), candidates.end());
           dfs(res, cur, candidates, target, 0);

           return vector<vector<int> > (res.begin(), res.end());
    }
    void dfs(set<vector<int> > &res, vector<int> &cur, vector<int> candidates, int target, int pos) {
        if (!cur.empty() && target == 0) {
            res.insert(cur);
            return;
        }
        for (int i = pos; i < candidates.size(); ++i) {
            if (target - candidates[i] >= 0) {
                cur.push_back(candidates[i]);
                dfs(res, cur, candidates, target - candidates[i], i);
                cur.pop_back();
            } else {
                break;
            }
        }
    }
};
时间: 2024-10-04 18:53:50

【Lintcode】135.Combination Sum的相关文章

【Lintcode】153.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. Example Given candidate set [10,1,6,7,2,1,5] 

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

【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