39. 组合总和 https://leetcode-cn.com/problems/combination-sum/
40. 组合总和 II https://leetcode-cn.com/problems/combination-sum-ii/
216. 组合总和 III https://leetcode-cn.com/problems/combination-sum-iii/
377. 组合总和 Ⅳ https://leetcode-cn.com/problems/combination-sum-iv/
这次对以上组合问题进行总结。题的主旨是对一个数组找到所有和为target的组合形式。题目要求不一,下面一一介绍。
数组无重复,每个数字可以被无限次使用,要求返回所有可能的情况(顺序不一样视作一种)。用dfs。
思路:
class Solution(object):
def __init__(self):
self.res = []
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
self.res = []
candidates.sort()
self.dfs(candidates, target, [])
return self.res
def dfs(self, candidates, val, temp):
if 0 == val:
self.res.append(temp)
return
if val < 0:
return
for i in range(len(candidates)):
if candidates[i] > val: return
if len(temp) > 0 and candidates[i] < temp[-1]:
continue
self.dfs(candidates, val - candidates[i], temp + [candidates[i]])
return
数组有重复,每个数字只能被使用一次,要求返回所有可能的情况(顺序不一样视作一种)。用dfs。
思路:
class Solution(object):
def combinationSum2(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
res = []
candidates.sort()
#print candidates
def dfs(val, path, lastind):
if val == 0:# and path not in res:
res.append(path)
return
last = None
for i in range(len(candidates)):
if len(path) > 0 and (candidates[i] < path[-1] or candidates[i] == path[-1] and i <= lastind): #改动
continue
if candidates[i] > val:break
if candidates[i] == last:#改动
continue
dfs(val - candidates[i], path + [candidates[i]], i)
last = candidates[i]
return
dfs(target, [], -1)
return res
数组被指定为range(1,10),但是要求返回到数组长度都为指定长度k。要求返回所有可能的情况(顺序不一样视作一种)。用dfs。
思路:
class Solution(object):
def combinationSum3(self, k, n):
"""
:type k: int
:type n: int
:rtype: List[List[int]]
"""
nums = range(1, 10)
res = []
def dfs(val, path, c):
if len(path) == c:
if val == 0:
res.append(path)
return
for i in range(len(nums)):
if len(path) > 0 and nums[i] <= path[-1]:
continue
if nums[i] > val:
return
dfs(val - nums[i], path + [nums[i]], c)
return
dfs(n, [], k)
return res
数组无重复,每个数字可以被无限次使用,要求返回所有可能的情况(顺序不一样视作不同)的数量。用dfs。
思路:
class Solution(object):
def combinationSum4(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
dp = [1]
nums.sort()
for i in range(1, target + 1):
temp = 0
for j in range(len(nums)):
if nums[j] > i:
break
temp += dp[i - nums[j]]
dp.append(temp)
#print dp
return dp[-1]
求数量一般按照递推思想来做,而求所有可能情况一般是dfs。