当前位置:网站首页>[backtracking] full arrangement leetcode46
[backtracking] full arrangement leetcode46
2022-06-30 21:47:00 【Breeze_】
Give an array without duplicate numbers nums , Back to its All possible permutations . You can In any order Return to the answer .
Example 1:Input :nums = [1,2,3] Output :[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2:Input :nums = [0,1] Output :[[0,1],[1,0]]
Example 3:Input :nums = [1] Output :[[1]]
link :https://leetcode.cn/problems/permutations
Very classic backtracking algorithm problem foundation , Consider the state of the recursive tree 、 What are variables and returns , In this question , The variables of the recursion tree are the variables of each recursion nums, The root node is itself , Each recursion reduces the number by one , The leaf node is nums When it's empty , Go back .
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
self.ret = []
'''NOTE 1. Thinking about recursive trees ( What are the root and intermediate nodes ) 2. Think about each state of the recursive tree (for The loop condition ) 3. Think about the variables of the recursive tree ( Formal parameters of the backtrace function ) 4. Think about what the leaf nodes of a recursive tree are ( Where to end return And save what ) '''
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
边栏推荐
猜你喜欢
Clickhouse native monitoring item, system table description
《ClickHouse原理解析与应用实践》读书笔记(3)
Five years after graduation, I wondered if I would still be so anxious if I hadn't taken the test
Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
周少剑,很少见
Ten security measures against unauthorized access attacks
Arcmap|assign values to different categories of IDS with the field calculator
全面认识痛风:症状、风险因素、发病机理及管理
pytorch geometric torch-scatter和torch-sparse安装报错问题解决
1-2 install and configure MySQL related software
随机推荐
1-17 express Middleware
. NETCORE redis geo type
The 16th Heilongjiang Provincial Collegiate Programming Contest
FreeRTOS record (IX. an example of a bare metal project transferring to FreeRTOS)
1-16 路由的概念
Look at the top 10 capabilities of alicloud cipu
Reading notes of Clickhouse principle analysis and Application Practice (3)
1-1 数据库的基本概念
Arcmap|assign values to different categories of IDS with the field calculator
京东与腾讯续签三年战略合作协议;起薪涨至26万元,韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条
asp. Net core JWT delivery
微服务链路风险分析
Sqlserver gets the data of numbers, Chinese and characters in the string
模板方法模式介绍与示例
Three techniques for reducing debugging time of embedded software
攻防演练中的防泄露全家福
1-20 pre inspection request
A comprehensive understanding of gout: symptoms, risk factors, pathogenesis and management
笔记【JUC包以及Future介绍】
《Dynamic Routing Between Capsules》论文学习总结