组合
77.组合
题目:给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的不同组合 。
class Solution :
def combine ( self, n: int , k: int ) - > List[ List[ int ] ] :
def dfs ( n, k, path, res, start_index) :
if k == 0 :
res. append( path[ : ] )
return
for i in range ( start_index, n+ 1 ) :
path. append( i)
dfs( n, k- 1 , path, res, i+ 1 )
path. pop( )
path, res = [ ] , [ ]
start_index = 1
dfs( n, k, path, res, start_index)
return res
print ( Solution( ) . combine( n= 4 , k= 2 ) )
216.组合总和3
题目:找出所有相加之和为 n 的 k 个数的不同组合 。只使用数字1到9 ;每个数字最多使用一次 。
class Solution :
def combinationSum3 ( self, k: int , n: int ) - > List[ List[ int ] ] :
def dfs ( k, n, path, res, start_index) :
if k == 0 and n == 0 :
res. append( copy. deepcopy( path) )
return
for i in range ( start_index, 10 ) :
path. append( i)
dfs( k- 1 , n- i, path, res, i+ 1 )
path. pop( )
path, res = [ ] , [ ]
start_index = 1
dfs( k, n, path, res, start_index)
return res
print ( Solution( ) . combinationSum3( k = 3 , n = 7 ) )
39.组合总和
题目:给定一个无重复元素 的数组nums和一个目标整数 target ,找出 nums中可以使数字和为 target 的所有不同组合 。nums中的同一个数字可以无限制重复被选取 。 输入:nums = [2,3,6,7], target = 7 输出:[[2,2,3],[7]]
class Solution :
def combinnationSum ( self, nums: List[ int ] , target: int ) - > List[ List[ int ] ] :
def dfs ( nums, path, res, start_index, target) :
if target == 0 :
res. append( copy. deepcopy( path) )
return
for i in range ( start_index, len ( nums) ) :
if nums[ i] <= target:
path. append( nums[ i] )
dfs( nums, path, res, i, target- nums[ i] )
path. pop( )
path, res = [ ] , [ ]
start_index = 0
dfs( nums, path, res, start_index, target)
return res
print ( Solution( ) . combinnationSum( nums= [ 2 , 3 , 6 , 7 ] , target= 7 ) )
40.组合总和2
题目:给定一个整数数组 nums和一个目标数 target ,找出 nums中可以使数字和为 target 的所有不同组合 。nums中的每个数字在每个组合中只能使用一次 。 输入: nums= [2,5,2,1,2], target = 5, 输出: [[1,2,2],[5]]
class Solution :
def combinationSum2 ( self, nums: List[ int ] , target: int ) - > List[ List[ int ] ] :
nums. sort( )
def dfs ( nums, path, res, start_index, target) :
if target == 0 :
res. append( copy. deepcopy( path) )
return
for i in range ( start_index, len ( nums) ) :
if i > start_index and nums[ i] == nums[ i- 1 ] :
continue
if nums[ i] <= target:
path. append( nums[ i] )
dfs( nums, path, res, i+ 1 , target- nums[ i] )
path. pop( )
path, res = [ ] , [ ]
start_index = 0
dfs( nums, path, res, start_index, target)
return res
print ( Solution( ) . combinationSum2( nums= [ 10 , 1 , 2 , 7 , 6 , 1 , 5 ] , target= 8 ) )
子集
78.子集
题目:给你一个无重复元素 整数数组 nums ,返回该数组所有可能的不同子集 。
class Solution :
def subsets ( self, nums: List[ int ] ) - > List[ List[ int ] ] :
def dfs ( nums, path, res, start_index) :
res. append( copy. deepcopy( path) )
for i in range ( start_index, len ( nums) ) :
path. append( nums[ i] )
dfs( nums, path, res, i+ 1 )
path. pop( )
path, res = [ ] , [ ]
start_index = 0
dfs( nums, path, res, start_index)
return res
print ( Solution( ) . subsets( nums = [ 1 , 2 , 3 ] ) )
90.子集2(不剪枝)
题目:给你一个可能包含重复元素 整数数组 nums ,请你返回该数组所有可能的不同子集 。
class Solution :
def subsets ( self, nums: List[ int ] ) - > List[ List[ int ] ] :
nums. sort( )
def dfs ( nums, path, res, start_index) :
if path not in res:
res. append( copy. deepcopy( path) )
return
for i in range ( start_index, len ( nums) ) :
path. append( nums[ i] )
dfs( nums, path, res, i+ 1 )
path. pop( )
path, res = [ ] , [ ]
start_index = 0
dfs( nums, path, res, start_index)
return res
print ( Solution( ) . subsets( nums = [ 1 , 2 , 3 ] ) )
90.子集2(剪枝)
class Solution :
nums. sort( )
def subsets ( self, nums: List[ int ] ) - > List[ List[ int ] ] :
def dfs ( nums, path, res, start_index) :
res. append( copy. deepcopy( path) )
for i in range ( start_index, len ( nums) ) :
if i > start_index and nums[ i] == nums [ i- 1 ] :
continue
path. append( nums[ i] )
dfs( nums, path, res, i+ 1 )
path. pop( )
path, res = [ ] , [ ]
start_index = 0
dfs( nums, path, res, start_index)
return res
print ( Solution( ) . subsets( nums = [ 1 , 2 , 3 ] ) )
全排列
46.全排列
题目:给定一个不含重复数字 的数组 nums ,返回其所有可能的不重复的全排列 。
class Solution :
def permute ( self, nums: List[ int ] ) - > List[ List[ int ] ] :
visited = [ False for _ in range ( len ( nums) ) ]
def dfs ( nums, path, res, visited, depth) :
if depth == len ( nums) :
res. append( copy. deepcopy( path) )
return
for i in range ( len ( nums) ) :
if not visited[ i] :
visited[ i] = True
path. append( nums[ i] )
dfs( nums, path, res, visited, depth+ 1 )
visited[ i] = False
path. pop( )
path, res = [ ] , [ ]
depth = 0
dfs( nums, path, res, visited, depth)
return res
print ( Solution( ) . permute( nums= [ 1 , 2 ] ) )
47.全排列2(不剪枝)
题目:给定一个可包含重复数字 的序列 nums ,按任意顺序返回所有不重复的全排列 。
class Solution :
def permute ( self, nums: List[ int ] ) - > List[ List[ int ] ] :
visited = [ False for _ in range ( len ( nums) ) ]
def dfs ( nums, path, res, visited, depth) :
if depth == len ( nums) :
if path not in res:
res. append( copy. deepcopy( path) )
return
for i in range ( len ( nums) ) :
if not visited[ i] :
visited[ i] = True
path. append( nums[ i] )
dfs( nums, path, res, visited, depth+ 1 )
visited[ i] = False
path. pop( )
path, res = [ ] , [ ]
depth = 0
dfs( nums, path, res, visited, depth)
return res
print ( Solution( ) . permute( nums= [ 1 , 2 ] ) )
47.全排列2(剪枝)
题目:给定一个可包含重复数字的序列 nums ,按任意顺序返回所有不重复的全排列。
class Solution :
def permute ( self, nums: List[ int ] ) - > List[ List[ int ] ] :
nums. sort( )
visited = [ False for _ in range ( len ( nums) ) ]
def dfs ( nums, path, res, visited, depth) :
if depth == len ( nums) :
res. append( copy. deepcopy( path) )
return
for i in range ( len ( nums) ) :
if i > 0 and nums[ i] == nums[ i- 1 ] and not visited[ i- 1 ] :
continue
if not visited[ i] :
visited[ i] = True
path. append( nums[ i] )
dfs( nums, path, res, visited, depth+ 1 )
visited[ i] = False
path. pop( )
path, res = [ ] , [ ]
depth = 0
dfs( nums, path, res, visited, depth)
return res
print ( Solution( ) . permute( nums= [ 1 , 2 ] ) )
总结:
数组中的元素是否重复
是:要考虑相同元素为首的重复集合(排序,剪枝) 否:不用剪枝
数组中的元素是否可以重复使用
是:下一次dfs从i开始 否:下一次dfs从i+1开始