class Solution:
# import itertools
# def permute(self, nums):
# """
# :type nums: List[int]
# :rtype: List[List[int]]
# """
# result = itertools.permutations(nums)
# result = [list(t) for t in result]
# return result
def permute(self, nums):
# DPS with swapping
res = []
if len(nums) == 0:
return res
self.get_permute(res, nums, 0)
return res
def get_permute(self, res, nums, index):
if index == len(nums):
res.append(list(nums))
return
for i in range(index, len(nums)):
nums[i], nums[index] = nums[index], nums[i]
# s(n) = 1 + s(n-1)
self.get_permute(res, nums, index + 1)
nums[i], nums[index] = nums[index], nums[i]
# def permute(self, nums):
# # iterative solution
# res = [[]]
# for i in range(len(nums)):
# cache = set()
# while len(res[0]) == i:
# curr = res.pop(0)
# for j in range(len(curr) + 1):
# # generate new n permutations from n -1 permutations
# new_perm = curr[:j] + [nums[i]] + curr[j:]
# stemp = ''.join(map(str, new_perm))
# if stemp not in cache:
# cache.add(stemp)
# res.append(new_perm)
# return res
python-leetcode面试题解之第46题全排列-题解.zip
需积分: 1 109 浏览量
2024-03-19
05:12:07
上传
评论
收藏 1015B ZIP 举报
m0_57195758
- 粉丝: 727
- 资源: 246
最新资源
- 3122080306 邹子轩 实验报告二.docx
- 基于STM32 NUCLEO板设计彩色LED照明灯(纯cubeMX开发)(大赛作品,文档完整,可直接运行)
- 发那科工业机器人保养大全
- Sphere.h
- REMD固有时间尺度分解信号分量可视化(Matlab完整源码和数据)
- 嵌入式系统双单片机STC89C52+STC15W104多功能学习板电路图可扩展 适用于单片机初学者和教学
- 基于STM32蓝牙控制小车系统设计(硬件+源代码+论文)大赛作品
- XILINXFPGA源码基于Spartan3火龙刀系列FPGA开发板VGA测试例程
- Java聊天室的设计与实现【尚学堂·百战程序员】
- python中matplotlib教程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈