当前位置:网站首页>377.组合总和 Ⅳ
377.组合总和 Ⅳ
2022-06-22 08:15:00 【zzu菜】
组合总和 Ⅳ
给你一个由 不同 整数组成的数组 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
- dp数组的定义及下标的含义
dp[i][j] : 表示前i个数组成背包j的组合数为dp[i][j]
- 状态转移方程
j<nums[i] , dp[i][j] = dp[i-1][j]
j≥nums[i], dp[i][j] = dp[i][j-nums[i]]+ dp[i][j-nums[i-1]]+…+ dp[i][j-nums[0]]
关于理解 j≥nums[i]时dp[i][j]的状态方程
因为这个是不分顺序并且是无穷背包 所以 前面的每个物品都有可能被装入背包
然后累加起来就是不分顺序的组合数
3. 初始化
初始第一行和第一列
第一列为1,因为背包为0时的组合数只有一个不取数即可。
package 力扣;
/** * @author yyq * @create 2022-06-21 18:18 */
public class leetcode377 {
public static void main(String[] args) {
leetcode377 leetcode377=new leetcode377();
leetcode377.combinationSum4(new int[]{
1,2,3},4);
}
public int combinationSum4(int[] nums, int target) {
// 创建dp数组
int[][] dp=new int[nums.length][target+1];
// 初始化dp数组
for (int i=0;i<nums.length;i++){
dp[i][0] = 1;
}
for (int j=1;j<target+1;j++){
if(j>=nums[0]){
if(j%nums[0]==0){
dp[0][j] = 1;
}
else dp[0][j] = 0;
}else dp[0][j] = 0;
}
// 填补dp数组
// 外层循环物品 内层背包
for (int i=1;i<nums.length;i++){
for (int j=1;j<target+1;j++){
if(j>=nums[i]){
for (int x=0;x<=i;x++){
dp[i][j] =dp[i][j] + dp[i][j-nums[x]];
}
}
else dp[i][j] = dp[i-1][j];
}
}
tools.printDP(dp,nums.length,target+1);
return dp[nums.length-1][target];
}
}
边栏推荐
- 【Oracle 數據庫】奶媽式教程 day13 日期函數
- Some suggestions on Oracle SQL query return optimization
- Calculation days ()
- Oracle execution plan analysis
- Node red sends wechat official account message (template message)
- The jdbcurl is configured correctly in the project, but the jdbcurl is the wrong path after the project is started
- Using KDJ metrics on MT4
- 并发线程池底层原理详解与源码分析
- 《守望先锋》阵亡镜头、全场最佳和亮眼表现是如何设计
- Summary of basic knowledge of Oracle database SQL statement II: data operation language (DML)
猜你喜欢

Chapter VIII web project testing (the end of this chapter)

Qt 错误提示1: invalid use of incomplete type ‘***‘

Node red sends wechat official account message (template message)

MySQL 8.0 under Linux forgets the password

Example of QT combox

并发三大特性1-可见性

SQL server changes the primary key index to a non clustered index

Enumerations, custom types, and swaggerignore in swagger

Type of sub database and sub table

Mt4/mql4 getting started to proficient in foreign exchange EA automatic trading tutorial - identify the emergence of the new K line
随机推荐
MySQL avoids the method of repeatedly inserting records (ignore, replace, on duplicate key update)
PostgreSQL source code (56) extensible type analysis expandedobject/expandedrecord
先锋期货安全么?期货开户都是哪些流程?期货手续费怎么降低?
MySQL transactions
Chmod Chmod command
How to handle root password forgetting in MySQL
How to create an index
方阵循环右移
LVS Technology Practice
Interview shock 59: can there be multiple auto increment columns in a table?
QT 自定义组合控件(类提升功能)
Dom4j+xpath parsing XML files
swagger中的枚举、自定义类型和swaggerignore
Oracle gets the working day time between two dates
MySQL master-slave replication
Stored procedures and functions of MySQL
Node red sends wechat official account message (template message)
[Oracle database] mammy tutorial day13 date function
[songhongkang MySQL database] [advanced chapter] [07] MySQL storage engine
Design skills of common table structure in database design