当前位置:网站首页>LeetCode_377_Combination Sum IV
LeetCode_377_Combination Sum IV
2022-08-02 12:31:00 【Fitz1318】
题目链接
题目描述
给你一个由 不同 整数组成的数组 nums ,和一个目标整数 target .请你从 nums 中找出并返回总和为 target 的元素组合的个数.
题目数据保证答案符合 32 位整数范围.
示例 1:
输入:nums = [1,2,3], target = 4
输出:7
解释:
所有可能的组合为:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
请注意,顺序不同的序列被视作不同的组合.
示例 2:
输入:nums = [9], target = 3
输出:0
提示:
1 <= nums.length <= 2001 <= nums[i] <= 1000nums中的所有元素 互不相同1 <= target <= 1000
进阶:如果给定的数组中含有负数会发生什么?问题会产生何种变化?如果允许负数出现,需要向题目中添加哪些限制条件?
解题思路
动态规划五部曲
确定
dp数组及其下标含义dp[i]:Gather together into a positive integeri的排列个数
确定递推公式
dp[i] += dp[i- nums[j]]
dp数组初始化dp[0] = 1- 其他初始化为0即可,Because will be cover behind
确定遍历顺序
- The number can be unlimited use,States that this is a completely knapsack problem
- 得到的集合是排列,Then use the outerforIterate over the backpack,内层for循环遍历物品
举例推到
dp数组
AC代码
class Solution {
public int combinationSum4(int[] nums, int target) {
int[] dp = new int[target + 1];
dp[0] = 1;
for (int i = 0; i <= target; i++) {
for (int j = 0; j < nums.length; j++) {
if (i >= nums[j]) {
dp[i] += dp[i - nums[j]];
}
}
}
return dp[target];
}
}
边栏推荐
- Drools(8): WorkBench uses
- simulink PID自动整定
- 技术分享| 融合调度系统中的电子围栏功能说明
- 不错的射击类js小游戏源码
- 三种实现分布式锁的方式
- np.nan, np.isnan, None, pd.isnull, pd.isna 整理与小结
- Free Chinese-English Translation Software - Automatic Batch Chinese-English Translation Software Recommended Daquan
- PHP+MYSQL【学生信息管理系统】(极简版)
- MD5详解(校验文件完整性)
- Seneor曝光基础知识
猜你喜欢

JVM学习----垃圾回收调优

免费文档翻译-免费批量文档翻译软件推荐

pig4cloud服务架构使用

Import and export data of SQL Server database

MyCat2的介绍与安装以及基本使用

免费的中英文翻译软件-自动批量中英文翻译软件推荐大全

np.nan, np.isnan, None, pd.isnull, pd.isna 整理与小结

FreeRTOS实验--一个函数创建多个任务

ssm access database data error

How to better assess credit risk?Just watch this scorecard model live
随机推荐
Pod Scheduling Strategy: Affinity, Stain and Stain Tolerance
1.3快速生成树协议RSTP
阿苹的思考
Hand rolled architecture, 41 Redis interview asked
FreeRTOS创建任务--动态创建、静态创建
Likou 977-Squaring of ordered arrays - brute force method & double pointer method
云原生(三十) | Kubernetes篇之应用商店-Helm介绍
力扣151-颠倒字符串中的单词
企业级数据治理工作怎么开展?Datahub这样做
JVM学习----垃圾回收调优
Lexicon 27 - Remove Elements - Simple Questions
NVIDIA NeMo Metrics 轻量性能采集系统
SQL Server 2019安装错误0x80004005 服务没有及时响应启动或控制请求详细解决方法
看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!
力扣704-二分查找
Pod调度策略:亲和性、污点与污点容忍
zabbix automated monitoring script
Create your own app applet ecosystem with applet containers
excel 批量翻译-excel 批量函数公司翻译大全免费
MD5详解(校验文件完整性)