leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0216.cpp (564B)
0 class Solution { 1 public: 2 vector<vector<int>> combinationSum3(int k, int n) { 3 vector<vector<int>> res; 4 vector<int> comb; 5 for (uint16_t i = 0; i < (1 << 9); i++) { 6 if (__builtin_popcount(i) != k) continue; 7 int crnt = i, total = 0; 8 9 comb.clear(); 10 while (crnt) { 11 comb.push_back(__builtin_ffs(crnt)); 12 total += comb.back(); 13 crnt &= crnt - 1; 14 } 15 if (total == n) res.push_back(comb); 16 } 17 return res; 18 } 19 };