Leetcode 377. Combination Sum IV

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

Example:

nums = [1, 2, 3]
target = 4

The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)

Note that different sequences are counted as different combinations.

Therefore the output is 7.

Follow up:

What if negative numbers are allowed in the given array?

How does it change the problem?

What limitation we need to add to the question to allow negative numbers?

例子:[1,2,3,5,6], target = 8。dp[8] 意为有多少种方式可以生成8。

例如: 8=1(in nums) + 7 = 2(in nums) + 5 = 3(in nums) + 4= 5(in nums) + 3 = 6(in nums) + 2。 所以dp[8] = dp[7] + dp[5] + dp[4] + dp[3] + dp[2]。

5=5(in nums) + 0 = 3(in nums) + 2 = 2(in nums) + 3 = 1(in nums) +4。 所以dp[5] = dp[0] + dp[2] + dp[4] + dp[4]。

 1 class Solution(object):
 2     def combinationSum4(self, nums, target):
 3         """
 4         :type nums: List[int]
 5         :type target: int
 6         :rtype: int
 7         """
 8         dp = [0]*(target + 1)
 9         dp[0] = 1
10         for i in range(target+1):
11             for n in nums:
12                 if i + n <= target:
13                     dp[i+n] += dp[i]
14         return dp[-1]
15             
时间: 2024-11-01 20:16:17

Leetcode 377. Combination Sum IV的相关文章

LC 377. Combination Sum IV

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target. Example: nums = [1, 2, 3] target = 4 The possible combination ways are: (1, 1, 1, 1) (1, 1, 2) (1,

377. Combination Sum IV

是看到discuss里面的解法,因为用backtracking实在太多可能性了 思路是和https://leetcode.com/problems/climbing-stairs/ 在climbing stairs里面假如有n个台阶,每次可以跨一个台阶或者两个台阶,那么它的状态转移方程是res[i] = res[i - 1] + res[i - 2],初始化是res[0] = 1; res[1] = 1; 但是在本题中,每次不再只是可以跨一步或者两步了,每次可以跨nums数组里面的任意数字的步,

377. Combination Sum IV 返回符合目标和的组数

[抄题]: Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target. Example: nums = [1, 2, 3] target = 4 The possible combination ways are: (1, 1, 1, 1) (1, 1, 2

377. Combination Sum IV 70. Climbing Stairs

back function (return number) remember the structure class Solution { int res = 0; //List<List<Integer>> resList = new ArrayList<List<Integer>>(); public int combinationSum4(int[] nums, int target) { Arrays.sort(nums); return back(

377. Combination Sum IV (DP)

1 class Solution { 2 public int combinationSum4(int[] nums, int target) { 3 int[] res = new int[target + 1]; 4 res[0] = 1; 5 for(int i = 1; i <= target; i++) { 6 for(int j = 0; j < nums.length; j++) { 7 if(nums[j] <= i && res[i - nums[j]]

[LeetCode][JavaScript]Combination Sum III

Combination Sum III Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascend

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

[LeetCode: 题解] Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will

[LeetCode] 039. Combination Sum (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 039. Combination Sum (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum/ 代码(github):https://github.com/illuz/leetcode 题意: 给出一些正整数集合,以及一个目标数,从集合中选择一