当前位置:网站首页>Leetcode 46 full arrangement
Leetcode 46 full arrangement
2022-07-24 04:28:00 【Luo Zhaocheng CSDN】
1. subject
Give an array without duplicate numbers nums , Back to its All possible permutations . You can In any order Return to the answer .
2. Analysis of problem solving ideas
This topic is the full arrangement topic of high school , Let's look at the rules first .
When the input nums = [1] when , There is only one , So there is only one sort [1].
When the input nums = [1, 2] when , Two figures , There are two ways to sort : [2, 1], [1, 2] . There are two new sorts , You can put it first. 1 Fixed , It's up there [1], here , Will be 2 Insert into [1] In this line , There are two ways , Insert index by 0 The location of , namely [2, 1], The other is to insert index by 1 The location of , namely [1, 2]
When the input nums = [1, 2, 3] when , As described in step 2 , The input of three numbers is the third number 3 Insert into all queues with only two numbers . therefore , The logic is simple , The first queue of two numbers is [2, 1], therefore 3 You can insert this pair of columns 0, 1, 2 Three places , Get... Separately [3, 2, 1], [2, 3, 1],[2, 1 , 3]. Similarly, we can get another three queues
According to this idea, it is easy to write the code .
3. Code implementation
Used here kotlin , The code is as follows :
fun permute(nums: IntArray): List<List<Int>> {
var result = ArrayList<List<Int>>()
for (item in nums) {
var tempResult = ArrayList<List<Int>>()
if (result.size <= 0) {
tempResult.add(listOf(item))
} else {
result.forEach {
listItem ->
for (it in 0..listItem.size) {
var newList = ArrayList<Int>()
newList.addAll(listItem)
newList.add(it, item)
tempResult.add(newList)
}
}
}
result = tempResult
}
return result
}
边栏推荐
- Game improvement of smart people: Chapter 3 Lesson 3: find game
- 在一线城市上班的程序员,家庭一般是怎样的?
- Clickpaas, a low code service provider, has completed a strategic merger with BiP technology to jointly build an industrial digital base
- [hope to answer] the data cannot be synchronized correctly
- 你有多久没有换新手机了?
- [translation] announce krius -- accelerate your monitoring and adoption of kubernetes
- Hardware knowledge 3 -- IIC protocol
- Combinatorial number (number of prime factors of factorials, calculation of combinatorial number)
- Privacy protection federal learning framework supporting most irregular users
- ARP Spoofing protection of network security
猜你喜欢

Ambire gas tank launches exclusive NFT launch
[hope to answer] the data cannot be synchronized correctly

基于C语言设计的一个医院叫号系统

别人发的word中的参考文献是{}这样的乱码格式怎么办

NFT除了买卖还能质押?

IPhone binding 163 mailbox solution

What if Adobe pr2022 doesn't have open subtitles?

What are the 10 live demos showing? It's worth watching again whether you've seen it or not

Exploration of new mode of code free production

Clickpaas, a low code service provider, has completed a strategic merger with BiP technology to jointly build an industrial digital base
随机推荐
(5) Digital electricity formula simplification method
(008) flask is OK if you have a hand -- database migration flask migrate
Energy principle and variational method note 11: shape function (a dimension reduction idea)
Once svchost Troubleshooting of exe process occupying a large amount of network bandwidth
[09] program loading: "640K memory" is really not enough?
Alibaba Taobao Department interview question: how does redis realize inventory deduction and prevent oversold?
Send data 1010_ 1. The number of bytes passed by the sender
Iqoo 10 series attacks originos original system to enhance mobile phone experience
1.7.1 right and wrong problem (infix expression)
Application scenarios and schemes of common mechanical equipment safety virtual simulation system
dispatch_ Once's Secret
如何用STATA进行chowtest
IPhone binding 163 mailbox solution
.gz的业务交互和对外服篇中我们通合多个模型
在一线城市上班的程序员,家庭一般是怎样的?
Jinglianwen technology provides 3D point cloud image annotation service
What is the general family of programmers working in first tier cities?
Robustness evaluation of commercial in vivo detection platform
Live broadcast preview | practice sharing of opengauss' autonomous operation and maintenance platform dbmind
eCB接口,其实质也 MDSemodet