首页 > 其他 > 详细

leetcode377 Combination Sum IV

时间:2017-09-07 20:04:36      阅读:232      评论:0      收藏:0      [点我收藏+]

思路:

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

原文:http://www.cnblogs.com/wangyiming/p/7491527.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!