Skip to content

Instantly share code, notes, and snippets.

@nlzy
Created November 15, 2020 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlzy/8e5f3773a37946f9f1434756c1d3ebad to your computer and use it in GitHub Desktop.
Save nlzy/8e5f3773a37946f9f1434756c1d3ebad to your computer and use it in GitHub Desktop.
class Solution {
public:
std::vector<std::vector<int>> combinationSum(std::vector<int>& candidates, int target) {
std::vector<std::vector<std::vector<int>>> dp(target + 1);
for (int i = 0; i < candidates.size(); i++) {
for (int j = 0; j <= target; j++) {
if (j == candidates[i]) {
dp[j].push_back(std::vector<int>{candidates[i]});
} else if (j > candidates[i]) {
for (auto& e : dp[j - candidates[i]]) {
dp[j].push_back(e);
dp[j].back().push_back(candidates[i]);
}
}
}
}
return dp[target];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment