当前位置:网站首页>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 <= 200
1 <= nums[i] <= 1000
nums
中的所有元素 互不相同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];
}
}
边栏推荐
- QListView的使用
- 阿苹的思考
- Data Lake (3): Hudi Concept Terminology
- SQL中字符串拼接
- Chapter 14 Manually create a REST service (2)
- 网站自动翻译-网站批量自动翻译-网站免费翻译导出
- 30 lines of code to realize serverless real-time health code recognition -- operation manual
- Seneor Exposure Basics
- Intelligent Image Analysis-Intelligent Home Appliance Image Target Detection Statistical Counting Detection and Recognition-iCREDIT
- 用位运算为你的程序加速
猜你喜欢
FreeRTOS--优先级实验
Drools(8): WorkBench uses
不错的射击类js小游戏源码
Openlayers 快速上手教程
网站自动翻译-网站批量自动翻译-网站免费翻译导出
自定义mvc框架复习
Distributed current limiting, hand & redisson implementation
Software component analysis: 5 major capabilities to protect software supply chain security
1.3 Rapid Spanning Tree Protocol RSTP
商业流程服务BPass你真的了解吗?
随机推荐
30行代码实现无服务器实时健康码识别--操作手册
After Effects 教程,如何在 After Effects 中对蒙版进行动画绘制?
力扣209-长度最小的字符串——滑动窗口法
According to the field classification Golang map array
从幻核疑似裁撤看如何保证NFT的安全
手撸架构,网络 面试36问
分布式限流利器,手撕&redisson实现
excel 批量翻译-excel 批量函数公司翻译大全免费
An example of type3 voltage loop compensator taking Boost as an example
js真3d柱状图插件
Transfer files between servers
Data Lake (3): Hudi Concept Terminology
30 lines of code to realize serverless real-time health code recognition -- operation manual
看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!
商业流程服务BPass你真的了解吗?
Distributed current limiting, hand & redisson implementation
MyCat2的介绍与安装以及基本使用
Seneor Exposure Basics
LeetCode_377_组合总和Ⅳ
OpenFeign设置header的3种方式