#46. 全排列
class Solution:
def permute(self, nums):
if len(nums) == 0:
return []
if len(nums) == 1:
return [[nums[0]]]
result = []
for i,v in enumerate(nums):
tmp = self.permute(nums[:i]+nums[i+1:])
for j in tmp:
result.append([v]+j)
return result
so = Solution()
print(so.permute([1,2,3]))
#47. 全排列 II
class Solution:
def permuteUnique(self, nums):
if len(nums) == 0:
return []
if len(nums) == 1:
return [[nums[0]]]
nums = sorted(nums)
result = []
flag = None
for i,v in enumerate(nums):
if v != flag:
flag = v
tmp = self.permuteUnique(nums[:i]+nums[i+1:])
for j in tmp:
result.append([v]+j)
else:
continue
return result
so = Solution()
print(so.permuteUnique([3,3,0,3]))
# 77. 组合
class Solution:
def generateCombinations(self, n , k ,start):
if k == 0:
return []
if k == 1:
return [[i] for i in range(start, n+1)]
result = []
for i in range(start, n+1):
#加入剪枝
if i > (n - k + 1):
break
tmp = self.generateCombinations(n, k-1, i+1)
for j in tmp:
result.append([i]+j)
return result
def combine(self, n, k):
result = self.generateCombinations(n, k, 1)
return result
so = Solution()
print(so.combine(4,2))
#39.组合总和
class Solution:
def __init__(self):
self.res = []
def generatecombination(self, i, candidates, target, tmp):
if target == 0:
self.res.append(tmp)
return
if (len(candidates) == i or target
self.generatecombination(i, candidates, target-candidates[i], tmp + [candidates[i]])
self.generatecombination(i+1, candidates, target, tmp)
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
candidates = sorted(candidates)
self.generatecombination(0, candidates, target,[])
return self.res
#40. 组合总和 II
class Solution:
def __init__(self):
self.res = []
def generatecombination(self, i, candidates, target, tmp):
if target == 0:
self.res.append(tmp)
return
if (len(candidates) == i or target
self.generatecombination(i+1, candidates, target-candidates[i], tmp + [candidates[i]])
self.generatecombination(i+1, candidates, target, tmp)
def combinationSum2(self, candidates, target):
candidates = sorted(candidates)
self.generatecombination(0, candidates, target,[])
result = []
for i in self.res:
if i in result:
continue
else:
result.append(i)
return result
#216. 组合总和 III
class Solution:
def __init__(self):
self.res = []
def generateCombinations(self, k, n, i, tmp):
if k == 0 and n == 0:
self.res.append(tmp)
return
if i > 9:
return
if i > n or k == 0:
return
self.generateCombinations(k-1, n-i, i+1, tmp+[i])
self.generateCombinations(k, n, i+1, tmp)
def combinationSum3(self, k, n):
self.generateCombinations(k, n, 1, [])
return self.res
so = Solution()
print(so.combinationSum3(3,9))
#78. 子集
class Solution:
def generateCombinations(self, nums , k ,start):
n = len(nums)
if k == 0:
return []
if k == 1:
return [[nums[index]] for index in range(start, n)]
result = []
for i in range(start, n+1):
#加入剪枝
if i > (n - k + 1):
break
tmp = self.generateCombinations(nums, k-1, i+1)
for j in tmp:
result.append([nums[i]]+j)
return result
def subsets(self, nums):
nums = sorted(nums)
res = []
for i in range(1, len(nums)+1):
tmp = self.generateCombinations(nums, i, 0)
res += tmp
res.insert(0, [])
return res
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
res = []
n = len(nums)
def helper(i, tmp):
res.append(tmp)
for j in range(i, n):
helper(j + 1,tmp + [nums[j]] )
helper(0, [])
return res
#90. 子集 II
class Solution:
def subsetsWithDup(self, nums):
nums = sorted(nums)
res = []
n = len(nums)
def helper(i, tmp):
if tmp not in res:
res.append(tmp)
for j in range(i, n):
helper(j + 1,tmp + [nums[j]] )
helper(0, [])
return res
so = Solution()
print(so.subsetsWithDup([1,2,2]))