当前位置:网站首页>[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
边栏推荐
- [untitled]
- PyTorch量化实践(1)
- Troubleshooting the problem of pytorch geometric torch scatter and torch spark installation errors
- 1-13 express listens to get and post requests & processes requests
- 【无标题】
- Prediction and regression of stacking integrated model
- PyTorch量化实践(2)
- Five years after graduation, I wondered if I would still be so anxious if I hadn't taken the test
- 1-3 使用SQL管理数据库
- Ten security measures against unauthorized access attacks
猜你喜欢

The Jenkins download Plug-in can't be downloaded. Solution
一文读懂什么是MySQL索引下推(ICP)

Random talk about Clickhouse join

興奮神經遞質——穀氨酸與大腦健康

Reading notes of Clickhouse principle analysis and Application Practice (3)

Summary of errors reported when using YML file to migrate CONDA environment

Open the jupyter notebook/lab and FAQ & settings on the remote server with the local browser

Clickhouse Native Monitoring item, System table Description

《ClickHouse原理解析与应用实践》读书笔记(2)

Anaconda下安装Jupyter notebook
随机推荐
一文读懂什么是MySQL索引下推(ICP)
Some problems when SSH default port is not 22
1-20 pre inspection request
测试勋章1234
1-15 nodemon
The 16th Heilongjiang Provincial Collegiate Programming Contest
1-13 express监听GET和POST请求&处理请求
12345
Akk bacteria - the next generation of beneficial bacteria
《ClickHouse原理解析与应用实践》读书笔记(3)
侧睡哈哈哈哈
笔记【JUC包以及Future介绍】
Document Layout Analysis: A Comprehensive Survey 2019论文学习总结
Fletter nested hell? No, constraintlayout to save!
1-21 jsonp interface
ceshi deces
Prediction and regression of stacking integrated model
1-7 Path路径模块
Sqlserver gets the data of numbers, Chinese and characters in the string
Go Web 编程入门: 一探优秀测试库 GoConvey