当前位置:网站首页>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
}
边栏推荐
- 你有多久没有换新手机了?
- Ship test / IMO a.799 (19) incombustibility test of marine structural materials
- Live video | 37 how to use starrocks to realize user portrait analysis in mobile games
- 由硬件确定(服务的服绍,可参看官方2 和
- Is cross modal semantic alignment optimal under comparative learning--- Adaptive sparse attention alignment mechanism IEEE trans MultiMedia
- Oracle的并行技术
- Shell syntax (1)
- Will your NFT disappear? Dfinity provides the best solution for NFT storage
- Traversal of binary tree
- Application scenarios and schemes of common mechanical equipment safety virtual simulation system
猜你喜欢

Iqoo 10 series attacks originos original system to enhance mobile phone experience

Go language series - synergy GMP introduction - with ByteDance interpolation

Qt5.14_MinGW/MSVC下实现VS2019面板自由拖拽组合功能

Where is the difficulty in attracting investment in the park? Inventory of difficulties and difficulties in attracting investment in industrial parks

C语言经典习题之编写一个程序,找出1000以内所有的完数。

C language classic exercises to write a program to find all the perfects within 1000.

嵌入式系统移植【6】——uboot源码结构
![[untitled]](/img/c1/23797dd628641d524b55a125e95c52.png)
[untitled]

From bio to realizing the function of simple multi person chat room -- IO model

智能合约:发布一种ERC20代币
随机推荐
.gz的业务交互和对外服篇中我们通合多个模型
What are the 10 live demos showing? It's worth watching again whether you've seen it or not
Shell语法(二)
Hardware knowledge 3 -- IIC protocol
Live classroom system 04 create service module
Text attack methods open source code summary
Traversal of binary tree
[untitled]
Live broadcast preview | practice sharing of opengauss' autonomous operation and maintenance platform dbmind
Export function called separately
Energy principle and variational method note 11: shape function (a dimension reduction idea)
How long has it been since you changed your cell phone?
The judges of C language classic exercises score the highest and lowest to get an average score
你有多久没有换新手机了?
BGP notes (II)
Design and implementation of data analysis platform for intelligent commerce
NFT除了买卖还能质押?
PostgreSQL source code learning (32) -- checkpoint ④ - core function createcheckpoint
Can NFT pledge in addition to trading?
Codeforces Round #808 (Div. 2) A - D