国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

LeetCode: Combination Sum

2019-11-11 01:41:40
字體:
供稿:網(wǎng)友

Given a set of candidate numbers (C)(without duplicates) and a target number (T), find all unique combinations inC 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.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]]
class Solution {    PRivate:    int index_count;    vector<vector<int> > results;    public:    void backtrace(int target, int sum, vector<int>& candidates, vector<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]]);            }            results.push_back(result);            return;        }                // To avoid repeat         for (int i = index[n]; i < candidates.size(); ++i)        {            index[n+1] = i;            backtrace(target, sum + candidates[i], candidates, index, n+1);        }    }        vector<vector<int>> combinationSum(vector<int>& candidates, int target) {        sort(candidates.begin(), candidates.end());                vector<int> real_cdts;        for (int i = 0; i < candidates.size(); ++i) {            if (candidates[i] <= target) {                real_cdts.push_back(candidates[i]);            }            else {                break;            }        }                index_count = 10000;        vector<int> index = vector<int>(index_count, 0);                results.clear();        backtrace(target, 0, real_cdts, index, 0);                return results;    }};
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 健康| 桑植县| 普洱| 岑巩县| 德保县| 黔西| 松滋市| 青川县| 丘北县| 彭泽县| 永善县| 莱阳市| 昌宁县| 镇原县| 张家口市| 元朗区| 西峡县| 东明县| 乐至县| 霞浦县| 卢龙县| 连平县| 凤城市| 甘洛县| 鹰潭市| 大埔县| 永昌县| 灌阳县| 淳化县| 高邑县| 恭城| 嵊泗县| 松溪县| 安平县| 建湖县| 惠安县| 合水县| 沙田区| 高淳县| 阜宁县| 包头市|