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

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

Leetcode: Combination Sum III

2019-11-11 01:00:57
字體:
來源:轉載
供稿:網友

Find all possible combinations of k numbers that add up to a numbern, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Example 1:

Input: k = 3, n = 7

Output:

[[1,2,4]]

Example 2:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]

Credits:Special thanks to @mithmatt for adding this PRoblem and creating all test cases.

Subscribe to see which companies asked this question.

class Solution {private:   int index_count;   vector<vector<int> > results;public:    void backtrace(int target, int sum, vector<int>& index, int id, int n, int k)    {        if (sum > target || n > k) {            return;        }        else if (sum == target && n == k) {            vector<int> result;            for(int i = 1; i <= n; ++i)            {                result.push_back(index[i]);            }            results.push_back(result);            return;        }                for (int i = id + 1; i < 10; ++i)        {            index[n+1] = i;            backtrace(target, sum + i, index, i, n+1, k);        }    }    vector<vector<int>> combinationSum3(int k, int n) {        if (k * 9 < n) {            return results;        }                vector<int> index = vector<int>(10, 0);        for (int i = 0; i < 10; ++i) {            index[i] = i;        }        results.clear();        backtrace(n, 0, index, 0, 0, k);                return results;                    }};


上一篇:JUC之AQS框架

下一篇:宏的試例程序

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乃东县| 鞍山市| 井冈山市| 莎车县| 翁源县| 化德县| 微山县| 翼城县| 泉州市| 文登市| 渑池县| 晴隆县| 班玛县| 上蔡县| 蛟河市| 元朗区| 武鸣县| 准格尔旗| 荃湾区| 东辽县| 凌云县| 大港区| 桂阳县| 深州市| 鄂州市| 清涧县| 芜湖市| 甘洛县| 安泽县| 海盐县| 西华县| 安多县| 阜新| 寿阳县| 阿城市| 呼伦贝尔市| 莱州市| 溧水县| 科技| 内乡县| 新源县|