def combinations(arr,num):
def solver(arr,num,ans,cur,s):
print(s,'cur',cur)
if len(cur)==num:
ans.append(cur[::])
return
for i in range(s,len(arr)):
cur.append(arr[i])
solver(arr,num,ans,cur,i+1)
cur.pop()
ans=[]
cur=[]
solver(arr,num,ans,cur,0)
return ans
def permutation(arr,num):
def solver(arr,num,ans,cur):
if len(cur)==num:
print(cur)
ans.append(cur[::])
return
for a in arr:
if a in cur:
continue
cur.append(a)
solver(arr,num,ans,cur)
cur.pop()
ans=[]
cur=[]
solver(arr,num,ans,cur)
return ans
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
if len(nums) <= 1:
return [nums]
res = []
for idx, num in enumerate(nums):
res_nums = nums[:idx] + nums[idx + 1:]
for j in self.permute(res_nums):
res.append([num] + j)
return res
if __name__=="__main__":
arr = [3,4,5,6]
num = 3
result = permutation(arr,num)
print(result)
输入重复数组如何处理??
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步