当前位置:网站首页>Force buckle 31 next arrangement
Force buckle 31 next arrangement
2022-06-11 18:26:00 【Yingtai night snow】
Power button 31 Next spread
Title Description
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 .
I/o sample
Input :nums = [1,2,3]
Output :[1,3,2]
Input :nums = [3,2,1]
Output :[1,2,3]
Input :nums = [1,1,5]
Output :[1,5,1]
Topic analysis
The description of this question can only say , Said and didn't say the same , It's not clear at all . Let's reinterpret the topic . The input given in this topic is an array nums=[1,2,3], We need to find the next array .
Does it sound silly , But if you look at this array as a continuous number 123 Well , What we need to find is that we need to find exactly the right ratio 123 Large but not overly large arrays are 132
According to this principle, we can write the adjacency relation of this number , It is a process of size comparison
123
132
213
231
312
321
So how do you find the right permutation , We need to analyze the increasing law of these adjacency relations .
- First of all, we should look from the back to the front , Find the first sequentially increasing number pair , such as 23
- Record , Coordinates of the first value of the number pair i, from i+1, To end, Find the first greater than in reverse order nums[i] Coordinates of values j
- In exchange for nums[i] and nums[j] Value
- take i Subsequent numbers are sorted in ascending order
- If the array inputs are all in reverse order , Then you can sort the original array incrementally .
Algorithm implementation , Two scans
void nextPermutation(vector<int>&nums)
{
int length=nums.size();
// When the string length of the container is less than or equal to 1 when , No sorting
if(length<=1)
{
return;
}
// Find the first increment in the array
int i=length-2;
for(i=length-2;i>=0;i--)
{
if(nums[i]<nums[i+1])
{
break;
}
}
// When the sequence is found to be decreasing, the first increasing array is returned
if(i==-1)
{
sort(nums.begin(),nums.end());
}
else
{
// Find the first one greater than i Coordinates of the elements of
int pos;
for( pos=length-1;pos>i;pos--)
{
if(nums[pos]>nums[i])
{
break;
}
}
swap(nums[i],nums[pos]);
// Yes i The following elements are sorted incrementally
sort(nums.begin()+i+1,nums.end());
}
}
边栏推荐
- 力扣34在排序数组中查找元素的第一个和最后一个位置
- 牛客刷题——part8
- On the problem that the while loop condition in keil does not hold but cannot jump out
- [golang] leetcode - 349 Intersection of two arrays (hash table)
- [c language] output the students within the specified score range with the structure
- 信号的处理与捕捉
- NFT platform development NFT mall source code NFT mall development chain game development
- System learning typescript (V) - joint type
- RadioGroup动态添加RadioButton
- 牛客刷题——part7
猜你喜欢
随机推荐
单选按钮 文字背景同时改变
Retrofit source code analysis
Use egg Js+mongodb simple implementation of curdapi
PIL-Pillow图像处理【1】-安装与新建
NR LDPC 打孔-punctured
Labelme for image data annotation
[Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)
TR-069 protocol introduction
使用Visdom对损失函数进行监控
力扣刷题——二叉树的层序遍历
General terms in security field
Understanding of distributed transactions
Common operations of Visio
On the problem that the while loop condition in keil does not hold but cannot jump out
高并发架构设计
Ubuntu installs PSQL and runs related commands
Several commands related to domain name
Say no to credit card fraud! 100 lines of code to realize simplified real-time fraud detection
力扣23题,合并K个升序链表
牛客刷题——二叉搜索树与双向链表









