当前位置:网站首页>LeetCode_377_组合总和Ⅳ
LeetCode_377_组合总和Ⅳ
2022-08-02 12:23: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 <= 200
1 <= nums[i] <= 1000
nums
中的所有元素 互不相同1 <= target <= 1000
进阶:如果给定的数组中含有负数会发生什么?问题会产生何种变化?如果允许负数出现,需要向题目中添加哪些限制条件?
解题思路
动态规划五部曲
确定
dp
数组及其下标含义dp[i]
:凑成正整数i
的排列个数
确定递推公式
dp[i] += dp[i- nums[j]]
dp
数组初始化dp[0] = 1
- 其他初始化为0即可,因为后面都会被覆盖掉
确定遍历顺序
- 个数可以无限使用,说明这是一个完全背包问题
- 得到的集合是排列,那么就用外层for循环遍历背包,内层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];
}
}
边栏推荐
- 企业级数据治理工作怎么开展?Datahub这样做
- Problem solving in the process of using mosquitto
- 网站自动翻译-网站批量自动翻译-网站免费翻译导出
- Leek 151 - Reverse words in a string
- WebUI自动化测试框架搭建从0到1(完整源码)更新完毕
- 项目监控六大事项
- Do you really understand the business process service BPass?
- How to connect TDengine through DBeaver?
- go源码之sync.Waitgroup
- darknet训练yolov4模型
猜你喜欢
ssm访问数据库数据报错
Taurus.MVC V3.0.3 Microservice Open Source Framework Released: Make the evolution of .NET architecture easier in large concurrency.
Basic protocol explanation
ssm access database data error
kvm部署
Lexicon 27 - Remove Elements - Simple Questions
基于深度学习的裂缝检测技术
【第六届强网杯CTF-Wp】
Likou 35 - search for insertion position - binary search
Data Lake (3): Hudi Concept Terminology
随机推荐
学习经验分享之七:YOLOv5代码中文注释
JVM学习----垃圾回收调优
免费文档翻译-免费批量文档翻译软件推荐
Metaverse "Drummer" Unity: Crazy expansion, suspense still exists
np.nan, np.isnan, None, pd.isnull, pd.isna finishing and summary
基于深度学习的裂缝检测技术
Golang map数组按字段分类
喜迎八一 《社会企业开展应聘文职人员培训规范》团体标准出版发行会暨橄榄枝大课堂上线发布会在北京举行
【MySQL】多表联合查询、连接查询、子查询「建议收藏」
doc2vec和word2vec(zigbee简介及应用)
手撸架构,Mysql 面试126问
Technology sharing | Description of the electronic fence function in the integrated dispatching system
手撸架构,网络 面试36问
基础协议讲解
Likou 35 - search for insertion position - binary search
Distributed current limiting, hand & redisson implementation
【Acunetix-Forgot your password】
智能图像分析-智能家用电器图像目标检测统计计数检测与识别-艾科瑞特科技(iCREDIT)
1.3快速生成树协议RSTP
数据湖(三):Hudi概念术语