原文地址:http://www.javashuo.com/article/p-dejhrktn-dd.html 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:spa
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?code
給定一個由正整數組成且不存在重複數字的數組,找出和爲給定目標正整數的組合的個數。htm
示例:blog
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。
進階:
若是給定的數組中含有負數會怎麼樣?
問題會產生什麼變化?
咱們須要在題目中添加什麼限制來容許負數的出現?get
8msit
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 }
12msio
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 }
12msclass
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 }