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

首頁 > 學院 > 開發設計 > 正文

LeetCode: Combination Sum II

2019-11-11 01:42:05
字體:
來源:轉載
供稿:網友

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inC 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 positive integers.The solution set must not contain duplicate combinations.

For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target8, A solution set is:

[  [1, 7],  [1, 2, 5],  [2, 6],  [1, 1, 6]]
class Solution {    PRivate:    int index_count;    vector<vector<int> > results;    public:    void backtrace(int target, int sum, vector<int>& candidates, vector<int>& index, int id, 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 = id + 1; i < candidates.size(); ++i)        {            index[n+1] = i;            backtrace(target, sum + candidates[i], candidates, index, i, n+1);            while(i < candidates.size() && candidates[i] == candidates[i+1]) {   // remove repeat numbers                i++;            }                    }    }        vector<vector<int>> combinationSum2(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, -1, 0);                return results;    }};
上一篇:LeetCode: Combination Sum

下一篇:poj1298

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 济阳县| 偏关县| 南宫市| 阿巴嘎旗| 兖州市| 边坝县| 上林县| 蛟河市| 准格尔旗| 剑河县| 灵宝市| 郯城县| 栖霞市| 通榆县| 苍溪县| 吉水县| 淮滨县| 香河县| 句容市| 石屏县| 江都市| 沾益县| 绩溪县| 新竹县| 敦化市| 黎川县| 吉安县| 突泉县| 峨边| 东源县| 西安市| 通海县| 祁阳县| 渑池县| 怀宁县| 山西省| 法库县| 蓬莱市| 彭泽县| 内江市| 嘉兴市|