当前位置:网站首页>【回溯】全排列 leetcode46
【回溯】全排列 leetcode46
2022-06-30 21:30:00 【小风_】
给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
示例 1:输入:nums = [1,2,3] 输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
示例 2:输入:nums = [0,1] 输出:[[0,1],[1,0]]
示例 3:输入:nums = [1] 输出:[[1]]
链接:https://leetcode.cn/problems/permutations
很经典的回溯算法题基础,需要考虑递归树的状态、变量和返回是什么,在此题中,递归树的变量是每一次递归时的nums,根节点是本身,每一次递归会减少一个数,叶子节点就是nums为空的时候,进行返回。
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
self.ret = []
'''NOTE 1.思考递归树(根节点和中间节点是什么) 2.思考递归树的每一个状态(for循环条件) 3.思考递归树的变量有哪些(回溯函数的形参) 4.思考递归树的叶子节点是什么(到哪结束return并保存什么) '''
def helper(nums:list,his:list):
if len(nums)==0:
self.ret.append(his)
return
for idx in range(len(nums)):
helper(nums[:idx]+nums[idx+1:],his+[nums[idx]])
helper(nums,[])
return self.ret
边栏推荐
- USBCAN分析仪的配套CAN和CANFD综合测试软件LKMaster软件解决工程师CAN总线测试难题
- Prediction and regression of stacking integrated model
- Understanding polymorphism
- 1-20 预检请求
- Dm8: generate DM AWR Report
- Personal developed penetration testing tool Satania
- 本地浏览器打开远程服务器上的Jupyter Notebook/Lab以及常见问题&设置
- 申请Vector 总线协议彩图壁纸挂画,非常棒哦!
- 漫谈Clickhouse Join
- 两个skyline
猜你喜欢

qsort函数和模拟实现qsort函数

Go build server Foundation

Personal developed penetration testing tool Satania

ICML2022 | 序列决策的效用理论

多态在代码中的体现

MySQL advanced 3

Adobe Photoshop (PS) - script development - remove file bloated script

SQL server extracts pure numbers from strings

时空数据挖掘:综述

Arcmap|assign values to different categories of IDS with the field calculator
随机推荐
Et la dégradation du modèle de génération de texte? Simctg vous donne la réponse
Apply for vector bus protocol color picture wallpaper hanging picture, very good!
vim 常用快捷键
做一个 Scrollbar 的思考
电子方案开发——智能跳绳方案
Spatiotemporal data mining: an overview
What does grade evaluation mean? What is included in the workflow?
Nacos部署及使用
介绍一款|用于多组学整合和网络可视化分析的在线平台
本地浏览器打开远程服务器上的Jupyter Notebook/Lab以及常见问题&设置
USBCAN分析仪的配套CAN和CANFD综合测试软件LKMaster软件解决工程师CAN总线测试难题
1-19 利用CORS解决接口跨域问题
Personal developed penetration testing tool Satania
Double solid histogram / double y-axis
ClickHouse distributed表引擎
Ten security measures against unauthorized access attacks
MySQL advanced 3
你我他是谁
.netcore redis GEO类型
Metauniverse may become a new direction of Internet development