377:组合总和 Ⅳ
This commit is contained in:
parent
d96b90471b
commit
e680e575eb
@ -0,0 +1,75 @@
|
|||||||
|
//给你一个由 不同 整数组成的数组 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
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 进阶:如果给定的数组中含有负数会发生什么?问题会产生何种变化?如果允许负数出现,需要向题目中添加哪些限制条件?
|
||||||
|
// Related Topics 动态规划
|
||||||
|
// 👍 391 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//377:组合总和 Ⅳ
|
||||||
|
public class CombinationSumIv {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new CombinationSumIv().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int combinationSum4(int[] nums, int target) {
|
||||||
|
int[] dp = new int[target + 1];
|
||||||
|
dp[0] = 1;
|
||||||
|
for (int i = 1; i <= target; i++) {
|
||||||
|
for (int j = 0; j < nums.length; j++) {
|
||||||
|
if (nums[j] <= i) {
|
||||||
|
dp[i] += dp[i - nums[j]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[target];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
<p>给你一个由 <strong>不同</strong> 整数组成的数组 <code>nums</code> ,和一个目标整数 <code>target</code> 。请你从 <code>nums</code> 中找出并返回总和为 <code>target</code> 的元素组合的个数。</p>
|
||||||
|
|
||||||
|
<p>题目数据保证答案符合 32 位整数范围。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [1,2,3], target = 4
|
||||||
|
<strong>输出:</strong>7
|
||||||
|
<strong>解释:</strong>
|
||||||
|
所有可能的组合为:
|
||||||
|
(1, 1, 1, 1)
|
||||||
|
(1, 1, 2)
|
||||||
|
(1, 2, 1)
|
||||||
|
(1, 3)
|
||||||
|
(2, 1, 1)
|
||||||
|
(2, 2)
|
||||||
|
(3, 1)
|
||||||
|
请注意,顺序不同的序列被视作不同的组合。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [9], target = 3
|
||||||
|
<strong>输出:</strong>0
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= nums.length <= 200</code></li>
|
||||||
|
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||||
|
<li><code>nums</code> 中的所有元素 <strong>互不相同</strong></li>
|
||||||
|
<li><code>1 <= target <= 1000</code></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>进阶:</strong>如果给定的数组中含有负数会发生什么?问题会产生何种变化?如果允许负数出现,需要向题目中添加哪些限制条件?</p>
|
||||||
|
<div><div>Related Topics</div><div><li>动态规划</li></div></div>\n<div><li>👍 391</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user