当前位置:网站首页>[leetcode] 31. Next arrangement
[leetcode] 31. Next arrangement
2022-07-24 09:13:00 【Xiaoqu classmate】
31、 Next spread
subject :
An integer array array Is to arrange all its members in sequence or linear order .
for example ,arr = [1,2,3] , The following can be regarded as arr Permutation :[1,2,3]、[1,3,2]、[3,1,2]、[2,3,1] .
Integer array Next spread It refers to the next lexicographic order of its integers . More formally , Arrange all the containers in the order from small to large , So the array of Next spread It is the arrangement behind it in this ordered container . If there is no next larger arrangement , Then the array must be rearranged to the lowest order in the dictionary ( namely , Its elements are arranged in ascending order ).
for example ,arr = [1,2,3] The next line up for is [1,3,2] .
Similarly ,arr = [2,3,1] The next line up for is [3,1,2] .
and arr = [3,2,1] The next line up for is [1,2,3] , because [3,2,1] There is no greater order of dictionaries .
Give you an array of integers nums , find nums The next permutation of .
must In situ modify , Only additional constant spaces are allowed .
Their thinking :
This question means :
“ Next spread ” Is defined as : The next larger order in the dictionary order of a given sequence of numbers . If there is no next larger arrangement , Then rearrange the numbers in the smallest order ( In ascending order ).
We can formally describe the problem as : Given several numbers , Combine them into an integer . How to rearrange these numbers , To get the next larger integer . Such as 123 The next larger number is 132. If there is no larger integer , The smallest integer is output .
With 1,2,3,4,5,6 For example , The order is :
123456
123465
123546
...
654321
You can see such a relationship :123456 < 123465 < 123546 < … < 654321.
The standard “ Next spread ” The algorithm can be described as :
- from Backward forward Find the first adjacent ascending element pair (i,j), Satisfy A[i] < A[j]. here [j,end) It must be in descending order
- stay [j,end) Find the first satisfaction from back to front A[i] < A[k] Of k.A[i]、A[k] They are the above 「 decimal 」、「 Large number 」
- take A[i] And A[k] In exchange for
- It can be concluded that at this time [j,end) It must be in descending order , Inversion [j,end), In ascending order
- If in step 1 No matching pair of adjacent elements found , Show the current [begin,end) For a descending order , Then jump to step 4
Reference code :
public void nextPermutation(int[] nums) {
int len = nums.length;
for (int i = len - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
Arrays.sort(nums, i, len);
for (int j = i; j <len; j++) {
if (nums[j] > nums[i - 1]) {
int temp = nums[j];
nums[j] = nums[i - 1];
nums[i - 1] = temp;
return;
}
}
}
}
Arrays.sort(nums);
return;
}

边栏推荐
- PIP3 installation complete with source
- How to open the port number of the server, and the corresponding port of common network services
- Definition and initialization of cv:: mat
- 我们说的组件自定义事件到底是什么?
- 科目1-3
- Vector control of permanent magnet synchronous motor (I) -- mathematical model
- Description of MATLAB functions
- What does CRM mean? Three "key points" for CRM management software selection
- Aruba learning notes 06 wireless control AC basic configuration (CLI)
- Redis learning - Introduction to redis and NiO principles
猜你喜欢

来阿里一年后我迎来了第一次工作变动....

How to import CAD files into the map new earth and accurately stack them with the image terrain tilt model

【汇编语言实战】一元二次方程ax2+bx+c=0求解(含源码与过程截屏,可修改参数)

Unity solves the package manager "you see to be offline"

03_ UE4 advanced_ illumination

How to judge and analyze NFT market briefly through NFT go?

Little dolphin "transformed" into a new intelligent scheduling engine, which can be explained in simple terms in the practical development and application of DDS

数据中台:始于阿里,兴于DaaS
![[assembly language practice] solve the unary quadratic equation ax2+bx+c=0 (including source code and process screenshots, parameters can be modified)](/img/5e/782e5c33accc455994aae044970431.png)
[assembly language practice] solve the unary quadratic equation ax2+bx+c=0 (including source code and process screenshots, parameters can be modified)

Android system security - 5.2-apk V1 signature introduction
随机推荐
SQL optimization principles
CUDA day 2: GPU core and Sm core components [easy to understand]
Introduction to common ansible modules
Linked list - 24. Exchange nodes in the linked list in pairs
Huawei wireless device security policy configuration command
DSP development, using CCS software to establish engineering and burning
利用opencv 做一个简单的人脸识别
Ansible 常用模块介绍
SQL server2012 installation method details [easy to understand]
Nuxt 路由切换后 asyncData 跨域报错
Detailed sequence traversal of leetcode102 binary tree
Scheme and software analysis of dual computer hot standby system "suggestions collection"
Leetcode102-二叉树的层序遍历详解
Wildcards in MySQL like statements: percent, underscore, and escape
Getting started with sorting - insert sorting and Hill sorting
Tiktok video traffic golden release time
Re6: reading paper licin: a heterogeneous graph based approach for automatic legal stat identification fro
【汇编语言实战】一元二次方程ax2+bx+c=0求解(含源码与过程截屏,可修改参数)
Tiktok shop will add a new site, and the Singapore site will be launched on June 9
Assignment operator (geritilent software - Jiuye training)