标签:
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