[Swift]LeetCode377. 组合总和 Ⅳ | Combination Sum IV

原文地址:https://www.cnblogs.com/strengthen/p/10278399.html 

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?


给定一个由正整数组成且不存在重复数字的数组,找出和为给定目标正整数的组合的个数。

示例:

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

所有可能的组合为:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)

请注意,顺序不同的序列被视作不同的组合。

因此输出为 7。

进阶:
如果给定的数组中含有负数会怎么样?
问题会产生什么变化?
我们需要在题目中添加什么限制来允许负数的出现?


 8ms

 1 class Solution {
 2     func combinationSum4(_ nums: [Int], _ target: Int) -> Int {       
 3         var memo = [Int](repeating: 0, count: target+1)
 4         memo[0] = 1        
 5         for num in 1...target {            
 6             for i in 0..<nums.count {                
 7                 if num >= nums[i] {                    
 8                     if memo[num] == Int.max - memo[num-nums[i]] { 
 9                         return 1                        
10                     }else if memo[num] > Int.max - memo[num-nums[i]]{ 
11                         return 0
12                     }                    
13                     memo[num] += memo[num-nums[i]]                    
14                 }
15             }
16         }        
17         return memo[target]
18     }
19 }

12ms

 1 class Solution {
 2     func combinationSum4(_ nums: [Int], _ target: Int) -> Int {
 3         var dp = [Int](repeating: -1, count: target + 1)
 4         dp[0] = 1
 5         return _helper(nums, target, &dp)
 6     }
 7     
 8     fileprivate func _helper(_ nums: [Int], _ target: Int, _ dp: inout [Int]) -> Int {
 9         if dp[target] != -1 {
10             return dp[target]
11         }
12         
13         var res = 0
14         for num in nums {
15             if target >= num {
16                 res += _helper(nums, target - num, &dp)
17             }
18         }
19         
20         dp[target] = res
21         
22         return res
23     }
24 }

12ms

 1 class Solution {
 2     func combinationSum4(_ nums: [Int], _ target: Int) -> Int {
 3         if nums.isEmpty
 4         {
 5             return 0
 6         }
 7         var map:[Int:Int] = [Int:Int]()
 8         return countCombination(nums, target, &map)        
 9     }
10     
11     func countCombination(_ nums: [Int], _ target: Int,_ map:inout[Int:Int]) -> Int
12     {
13         if map[target] != nil
14         {
15             return map[target]!
16         }
17         var result:Int = 0
18         for i in 0..<nums.count
19         {
20             if nums[i] < target
21             {
22                 result += countCombination(nums, target - nums[i], &map)
23             }
24             else if nums[i] == target
25             {
26                 result += 1
27             }
28         }
29         map[target] = result
30         return result
31     }
32 }

20ms

 1 class Solution {
 2     
 3     var cache:[Int:Int] = [:]
 4     var nums:[Int] = []
 5     
 6     func combinationSum4(_ nums: [Int], _ target: Int) -> Int {
 7         return combinationSum(nums, target)        
 8         // return combs.count
 9     }
10     
11     func combinationSum(_ nums:[Int], _ target: Int) -> Int {
12         
13         if self.cache[target] == nil {
14             var combs:Int = 0
15 
16             for num in nums {
17                 if num == target {
18                     combs += 1
19                 } 
20 
21                 if num < target {
22                     combs += combinationSum(nums, target-num)
23                 }
24             }
25 
26             self.cache[target] = combs
27         }
28         
29         return self.cache[target]!
30     }
31 }

24ms

 1 class Solution {
 2     var cache = [Int: Int]()
 3     func combinationSum4(_ nums: [Int], _ target: Int) -> Int {
 4         cache = [:]
 5         return helper(nums, target)
 6     }
 7     
 8     private func helper(_ nums: [Int], _ target: Int) -> Int {
 9         guard target >= 0 else { return 0 }
10         guard target > 0 else { return 1 }
11         guard cache[target] == nil else { return cache[target]! }
12         
13         let result = nums.map { helper(nums, target - $0) }.reduce(0) { $0 + $1 }
14         cache[target] = result
15         
16         return result
17     }
18 }

 

转载于:https://www.cnblogs.com/strengthen/p/10278399.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值