1. 程式人生 > >leetcode377 Combination Sum IV

leetcode377 Combination Sum IV

leet ron public code span strong col urn i++

思路:

dp。

實現:

 1 class Solution 
 2 {
 3 public:
 4     int combinationSum4(vector<int>& nums, int target) 
 5     {
 6         if (nums.empty()) return target == 0;
 7         vector<int> dp(target + 1, 0);
 8         dp[0] = 1;
 9         vector<int> tmp(nums.begin(), nums.end());
10 sort(tmp.begin(), tmp.end()); 11 int n = tmp.size(); 12 for (int i = 1; i <= target; i++) 13 { 14 for (int j = 0; tmp[j] <= i && j < n; j++) 15 { 16 dp[i] += dp[i - tmp[j]]; 17 } 18 } 19 return
dp[target]; 20 } 21 };

leetcode377 Combination Sum IV