当前位置:网站首页>[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
边栏推荐
- Sqlserver string type converted to decimal or integer type
- 1-18 create the most basic express server & API module for creating routes
- 1-16 路由的概念
- A group of K inverted linked lists
- Arcmap|assign values to different categories of IDS with the field calculator
- Internet of things botnet gafgyt family and backdoor vulnerability exploitation of Internet of things devices
- 1-1 数据库的基本概念
- sdfsdf
- 《ClickHouse原理解析与应用实践》读书笔记(3)
- clickhouse原生監控項,系統錶描述
猜你喜欢

Deployment and use of Nacos

【无标题】

A comprehensive understanding of gout: symptoms, risk factors, pathogenesis and management

Phoenix architecture: an architect's perspective

兴奋神经递质——谷氨酸与大脑健康

Ten security measures against unauthorized access attacks

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

Multi table operation - foreign key constraint

漫谈Clickhouse Join

Jupyter notebook/lab switch CONDA environment
随机推荐
Auto-created primary key used when not defining a primary key
1-15 nodemon
1-10 respond to client content according to different URLs
【无标题】第一次参加csdn活动
Export El table as is to excel table
Random talk about Clickhouse join
Installing jupyter notebook under Anaconda
PyTorch量化感知训练(QAT)步骤
介绍一款|用于多组学整合和网络可视化分析的在线平台
艾芬医生事件解析
Oprator-1 first acquaintance with oprator
开发属于自己的包
1-3 using SQL to manage databases
请问,启牛证券开户,可以开户吗?安全吗?你想要的答案全在这里
1-16 路由的概念
1-3 使用SQL管理数据库
周少剑,很少见
Reading notes of Clickhouse principle analysis and Application Practice (3)
Internet of things botnet gafgyt family and backdoor vulnerability exploitation of Internet of things devices
攻防演练中的防泄露全家福