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

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

LeetCode: Combination Sum II

2019-11-11 02:52: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;    }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永嘉县| 凯里市| 泸州市| 沛县| 额济纳旗| 安徽省| 普安县| 景德镇市| 翁牛特旗| 屏山县| 长阳| 金寨县| 沽源县| 邹城市| 镇平县| 黎川县| 蒙山县| 永善县| SHOW| 开江县| 兴安盟| 广元市| 洞头县| 钦州市| 萨嘎县| 伊金霍洛旗| 宁德市| 江津市| 孟村| 秦皇岛市| 柳河县| 华容县| 海淀区| 德令哈市| 大同市| 天津市| 上蔡县| 陆河县| 根河市| 简阳市| 阳高县|