目录
学习目标
491.递增子序列
46.全排列
47.全排列 II
学习内容
491.递增子序列
491. 递增子序列 - 力扣(LeetCode)https://leetcode.cn/problems/non-decreasing-subsequences/
class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
res = []
tmp = [-101]
n = len(nums)
def dfs(i):
if len(tmp)>=3:
res.append(tmp[1:])
if i==n:
return
s =set()
for j in range(i,n):
if nums[j]>=tmp[-1] and nums[j] not in s:
s.add(nums[j])
tmp.append(nums[j])
dfs(j+1)
tmp.pop()
dfs(0)
return res
46.全排列
46. 全排列 - 力扣(LeetCode)https://leetcode.cn/problems/permutations/
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
res = []
tmp = []
n = len(nums)
def dfs(i):
if i==n:
res.append(tmp.copy())
return
for j in range(n-i):
t = nums[j]
tmp.append(nums[j])
nums.pop(j)
dfs(i+1)
tmp.pop()
nums.insert(j,t)
dfs(0)
return res
47.全排列 II
47. 全排列 II - 力扣(LeetCode)https://leetcode.cn/problems/permutations-ii/
class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
res = []
tmp = []
n = len(nums)
nums.sort()
def dfs(i):
if i==n:
res.append(tmp.copy())
return
for j in range(n-i):
if j>0 and nums[j]==nums[j-1]:
continue
t = nums[j]
tmp.append(nums[j])
nums.pop(j)
dfs(i+1)
tmp.pop()
nums.insert(j,t)
dfs(0)
return res