491.递增子序列
class Solution:
def backtracking(self, nums, startIndex, path, result):
if len(path)>1:
result.append(path[:])
uset = set()
for i in range(startIndex, len(nums)):
if (not path or path[-1] <= nums[i]) and (nums[i] not in uset):
path.append(nums[i])
uset.add(nums[i])
self.backtracking(nums, i + 1, path, result)
path.pop()
else:
continue
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
result = []
self.backtracking(nums, 0, [], result)
return result
46.全排列
class Solution:
def backtracking(self,nums, path, used, result):
if len(path)==len(nums):
result.append(path[:])
for i in range(len(nums)):
if used[i]:
continue
path.append(nums[i])
used[i]=True
self.backtracking(nums, path, used, result)
used[i]=False
path.pop()
def permute(self, nums: List[int]) -> List[List[int]]:
result = []
self.backtracking(nums, [], [False] * len(nums), result)
return result
47.全排列 II
class Solution:
def backtracking(self,nums, path, used, result):
if len(path)==len(nums):
result.append(path[:])
return
for i in range(len(nums)):
if (i>0 and nums[i]==nums[i-1] and not used[i-1]) or used[i]:
continue
path.append(nums[i])
used[i]=True
self.backtracking(nums, path, used, result)
used[i]=False
path.pop()
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
result = []
nums.sort()
used_range=[False] * len(nums)
self.backtracking(nums, [], [False] * len(nums), result)
return result