Combination Sum

一遇到这种回溯递归的,感觉脑子就成了浆糊~~

第一点:对于不合格的元素直接返回,不在结果向量里添加任何东西,有了返回便继续下面的循环

第二点:对于某些元素可以重复无数次--采用办法下次递归的元素仍从接着上个元素。

for(int j=index[n];j<candidates.size()&&(target>=candidates[j]);j++)
        {
            index[n+1]=j; 


class Solution {
    private:
        vector<vector<int>> res;
        const int index_count=10000;

public:
    void findSet(int sum,vector<int>& candidates,int target,int index[],int n )
    {
        if(sum>target)
        return;
        if(sum==target)
        {
            vector<int> result;
            for(int i=1;i<=n;i++)
            {
                result.push_back(candidates[index[i]]);
            }
            res.push_back(result);
            return;
        }
        for(int j=index[n];j<candidates.size()&&(target>=candidates[j]);j++)
        {
            index[n+1]=j;
            findSet(sum+candidates[j],candidates,target,index,n+1);
        }
    }
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        sort(candidates.begin(),candidates.end());
        int *index=new int [index_count];
         memset(index,0,sizeof(int)*index_count);
        res.clear();
        findSet(0,candidates,target,index,0);delete[] index;
        return res;
    }
};
时间: 2024-12-19 14:41:47

Combination Sum的相关文章

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

leetcode笔记:Combination Sum II

一. 题目描述 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 once number of times. Note: ? All numbers (including target)

【转载】Combination Sum

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】Combination Sum

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]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/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

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

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][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][Python]40: Combination Sum II

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 40: Combination Sum IIhttps://oj.leetcode.com/problems/combination-sum-ii/ Given a collection of candidate numbers (C) and a target number (T),find all unique combinations in C where the candi