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

推荐订阅源

T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
雷峰网
雷峰网
量子位
有赞技术团队
有赞技术团队
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
博客园 - Franky
罗磊的独立博客
宝玉的分享
宝玉的分享
博客园_首页
腾讯CDC
The GitHub Blog
The GitHub Blog
D
DataBreaches.Net
IT之家
IT之家
D
Docker
Microsoft Security Blog
Microsoft Security Blog
博客园 - 司徒正美
V
V2EX
月光博客
月光博客
N
Netflix TechBlog - Medium
爱范儿
爱范儿
I
InfoQ
P
Proofpoint News Feed

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

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