0216.cpp (564B)
1 class Solution { 2 public: 3 vector<vector<int>> combinationSum3(int k, int n) { 4 vector<vector<int>> res; 5 vector<int> comb; 6 for (uint16_t i = 0; i < (1 << 9); i++) { 7 if (__builtin_popcount(i) != k) continue; 8 int crnt = i, total = 0; 9 10 comb.clear(); 11 while (crnt) { 12 comb.push_back(__builtin_ffs(crnt)); 13 total += comb.back(); 14 crnt &= crnt - 1; 15 } 16 if (total == n) res.push_back(comb); 17 } 18 return res; 19 } 20 };