惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
月光博客
月光博客
腾讯CDC
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Martin Fowler
Martin Fowler
A
About on SuperTechFans
博客园 - 叶小钗

博客园 - 来自海边的一片云

Binary Tree Maximum Path Sum 解题注意 CodingTMD’s Reading List De Bruijn 序列生成 Word Ladder I ,II 解题思路 suduko及8皇后问题及相关问题的解题思路 leetcode Word Break II 解题思路 Search for a string in an infinite stream of input string. 内存管理 Permutations leetcode Clone Graph leetcode 开弓没有回头箭 Combinations leetcode 组合问题 word break leetcode LRU cache Leetcode 重新试着写blog SQL injection Fuzz testing XML库的解析效率 Init()
combination sum leetcode
来自海边的一片云 · 2014-02-08 · via 博客园 - 来自海边的一片云

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 be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]

这个题目是个backtrack的问题,这个问题,也类似于printtree中path等于固定值的解法。

其中最有意思的一个trick是 记录访问过路径的index[] 以及那个对应的position。我的第一想法是直接用一个vector来记录路径,进来一个push,回退一个popback。

最后发现这是个很笨拙的方法。最好的办法是,用一个数组index 来记录位置,用递归的层数来做索引,将简单很多。

具体这个题目,两种思路,一个是sum的方法,从sum=0 开始,把整个数组内容递归的加一遍,知道发现target,那就把路径输出。

另外一个办法是减法,

void Find(vector<int> &candidates, int sum, int target, int* index, int start)
{
    int size = candidates.size();
    if (sum>target) return;

    if (sum == target)
    {
        vector<int> result;
        for (int i = 1; i<= start; i++)
        {
            result.push_back(candidates[index[i]]);
        }
        sort(result.begin(), result.end());

        finalresult.push_back(result);
        return;
    }

    for (int k = index[start]; k< size; k++)
    {
        index[start + 1] = k;
        Find(candidates, sum + candidates[k], target, index, start + 1);
    }

}

vector<vector<int> > combinationSum(vector<int> &candidates, int target) {

    int index[1024];
    index[0] = 0;
    int start = 0;
    int sum = 0;
    Find(candidates, sum, target, index, start);

    return finalresult;
}

减法

vector<vector<int>> finalresult;
void getcombine(vector<int>candidates, int target, int* index, int start)
{
    int len = candidates.size();
    

    if (target<candidates[0])//the target not fall inside means the sequence is wrong.
    {
        return;
    }
    else 
    {
        for (int i = index[start]; i < len; i++)
        {
            index[start + 1] = i;
            if (candidates[i] == target)// find it
            {
                vector<int> result;
                //get output from index
                for (int n = 1; n <= start+1; n++)
                {
                    result.push_back(candidates[index[n]]);
                }
                finalresult.push_back(result);
                return;
            }
            getcombine(candidates, target - candidates[i], index, start+1);
        }

    }


}
vector<vector<int> > combinationSumnew(vector<int> &candidates, int target) {
    vector<int> result;
    int index[1024];
    index[0] = 0;
    int start = 0;
    
    getcombine(candidates, target, index, start);
    
    return finalresult;
}