为有牺牲多壮志,敢教日月换新天。

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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10278399.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }

 

posted @ 2019-01-16 17:49  为敢技术  阅读(267)  评论(0编辑  收藏  举报