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

首頁 > 學院 > 開發(fā)設計 > 正文

LeetCode: Combination Sum II

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

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;    }};
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 中西区| 洪泽县| 日照市| 辽中县| 鄯善县| 蓬莱市| 洪江市| 通化县| 黄石市| 汽车| 平乡县| 哈尔滨市| 建始县| 资溪县| 丽水市| 固安县| 台中县| 巴东县| 宁夏| 津南区| 武川县| 巴楚县| 会昌县| 汉中市| 广灵县| 罗江县| 玉龙| 霍林郭勒市| 苍山县| 诏安县| 衡山县| 佛坪县| 陆丰市| 乌海市| 江川县| 太湖县| 务川| 永康市| 洛扎县| 峨山| 湾仔区|