Created
November 15, 2020 19:34
-
-
Save nlzy/8e5f3773a37946f9f1434756c1d3ebad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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