当前位置:网站首页>LeetCode 46 全排列
LeetCode 46 全排列
2022-07-24 04:27:00 【罗昭成-csdn】
1. 题目
给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
2. 题解思路分析
本题目就是高中的全排列题目,先来看规律。
当输入 nums = [1] 时,就只有一个,因此就只有一种排序方式 [1]。
当输入 nums = [1, 2] 时,两个数字,有两种排序的方式: [2, 1], [1, 2] 。新出现的两种排序,可以先把 1 固定下来,就是上面的 [1], 此时,在将 2 插入到 [1] 这个队列中,有两种方式,插入 index 为 0 的位置,即 [2, 1], 另一种是插入 index 为 1 的位置, 即 [1, 2]
当输入 nums = [1, 2, 3] 时,如第二步描述,三个数的输入就是将第三个数 3 分别插入只有两个数组成的所有队列中。 所以,逻辑很简单,两个数的第一个队列为 [2, 1], 因此 3 可以插入这个对列 0, 1, 2 三个位置,分别得到 [3, 2, 1], [2, 3, 1],[2, 1 , 3]。同理可得到另个三个队列
按照这个思路就可以很容易把代码写出来了。
3. 代码实现
此处使用的 kotlin , 代码如下:
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
}
边栏推荐
- Engineer competency model and skill requirements
- Smart people's game improvement: Chapter 3 Lesson 3 example: the secret of prime
- How long has it been since you changed your cell phone?
- MOS摄像化、数字化”包含指定(contro.熟练的
- Text attack methods open source code summary
- ARP Spoofing protection of network security
- 组合数(阶乘的质因子的个数,组合数的计算)
- C主机对IIC从来分别设置每足够的话,可下几位
- 如何用STATA进行chowtest
- Embedded system transplantation [6] - uboot source code structure
猜你喜欢

002_ Kubernetes installation configuration

An accident caused by MySQL misoperation, and "high availability" can't withstand it

Alibaba Taobao Department interview question: how does redis realize inventory deduction and prevent oversold?

Go language series - synergy GMP introduction - with ByteDance interpolation

MySQL service 1 master 2 slave, master master, MHA configuration detailed steps

(零八)Flask有手就行——数据库迁移Flask-Migrate

Codeforces Round #809 (Div. 2) A - D1

Shell syntax (1)

一次线上事故,我顿悟了异步的精髓

Leetcode 20 valid parentheses, 33 search rotation sort array, 88 merge two ordered arrays (nums1 length is m+n), 160 intersecting linked list, 54 spiral matrix, 415 character addition (cannot be direc
随机推荐
How to perform chowtest with Stata
[untitled]
PMIX ERROR: ERROR in file gds_ds12_lock_pthread.c
数组力扣(持续更新)
Determined by hardware (see official 2 and
(008) flask is OK if you have a hand -- database migration flask migrate
致-.-- -..- -
基于C语言设计的一个医院叫号系统
Leetcode 20 valid parentheses, 33 search rotation sort array, 88 merge two ordered arrays (nums1 length is m+n), 160 intersecting linked list, 54 spiral matrix, 415 character addition (cannot be direc
Shell syntax (2)
The second anniversary of open source, opengauss Developer Day 2022 full highlights review!
可能有点用的冷知识
MOS cameraization and digitization "includes designation (contro. skilled
C语言经典习题之猴子吃桃问题
Chapter III query processing of PostgreSQL Guide - Insider exploration
Oracle的并行技术
C语言经典习题之评委打分去掉最高最低求平均分
Qt5.14_ Realize the free drag and drop combination function of vs2019 panel under mingw/msvc
The judges of C language classic exercises score the highest and lowest to get an average score
Redis sentinel mode, master node check script