码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode216-Combination Sum III

时间:2016-05-15 18:05:42      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

https://leetcode.com/problems/combination-sum-iii/

用dfs枚举。

class Solution {
public:
    int kk, nn;
    bool has[10] = {false};
    vector<vector<int>> res;
    vector<vector<int>> combinationSum3(int k, int n) {
        kk = k, nn = n;
        vector<int> r;
        dfs(r, 0, 0);
        return res;
    }
    
 void dfs(vector<int> a, int t, int sum) {
    if (t != 0) {
        a.push_back(t);
        sum += t;
    }

    if (a.size() >= kk || sum >= nn) {
        if (a.size() == kk && sum == nn) {
            res.push_back(a);
        }
        return;
    }
    for (int i = t + 1; i < 10; ++i) {
        if (!has[i]) {
            has[i] = true;
            dfs(a, i, sum);
            has[i] = false;
        }
    }
}
};

 

leetcode216-Combination Sum III

标签:

原文地址:http://www.cnblogs.com/fripside/p/5495549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!