当前位置:网站首页>Click: 377. Combined Sum Ⅳ
Click: 377. Combined Sum Ⅳ
2022-08-10 00:33:00 【empty__barrel】
题目:
给你一个由 不同 整数组成的数组 nums ,和一个目标整数 target .请你从 nums 中找出并返回总和为 target 的元素组合的个数.
题目数据保证答案符合 32 位整数范围.
解析:
dp数组含义:
dp[j]: 凑成目标正整数为i的排列个数为dp[i]
递推公式:
dp[i] += dp[i - nums[j]];
初始化:
初始化为0,这样才不会影响dp[i]累加所有的dp[i - nums[j]]
遍历顺序:
This is a complete backpack,So it's a sequential traversal,But the test cases given by this topic show thatdpThe sequences in are permutations,So pay attention to twoforThe order in which loops are nested
- 如果求组合数就是外层for循环遍历物品,内层for遍历背包
- 如果求排列数就是外层for遍历背包,内层for循环遍历物品
题目数据保证答案符合 32 位整数范围
- dp[j] < INT_MAX - dp[j - nums[i]]So there is this judgment condition
代码:
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
vector<int>dp(target+1,0);
dp [0] = 1;
int bagweight = target;
for(int j = 0; j <= bagweight; ++j){
for(int i = 0; i < nums.size(); ++i){
if(j >= nums[i] && dp[j] < INT_MAX - dp[j - nums[i]]) dp[j] += dp[j-nums[i]];
}
}
return dp[target];
}
};
边栏推荐
猜你喜欢

Qt message mechanism and events

The 2022-8-9 sixth group of input and output streams

complete knapsack theory

kubesphere

干货!迈向鲁棒的测试时间适应
![[Interface Test] Decoding the request body string of the requests library](/img/99/82ef792dacd398a8a62dd94f235a91.png)
[Interface Test] Decoding the request body string of the requests library

如何正则匹配乱码?

matplotlib散点图颜色分组图例

如何知道电脑开机记录?

2022-08-09 mysql/stonedb-subquery performance improvement-introduction
随机推荐
JS--hashchange事件--使用/教程
UNI-APP_ monitor page scroll h5 monitor page scroll
Controller层代码这么写,简洁又优雅!
tiup cluster scale-out
PyQt5: Getting Started Tutorial
力扣:322. 零钱兑换
如何知道电脑开机记录?
友元类和友元函数
Leetcode 701. 二叉搜索树中的插入操作
leetcode 20. Valid Parentheses 有效的括号(中等)
2022-08-09 mysql/stonedb-subquery performance improvement-introduction
高手这样看现货白银走势图
torch.distributed多卡/多GPU/分布式DPP(二)——torch.distributed.all_reduce(reduce_mean)&barrier&控制进程执行顺序&随机数种子
“我“是一名测试/开发程序员,小孙的内心独白......
全面解析FPGA基础知识
6款跨境电商常用工具汇总
Leetcode 98. 验证二叉搜索树
技术盛宴!华云数据携六大议题亮相OpenInfra Days China
如何坚持使用程序化系统?
Mysql集群 ShardingSphere