当前位置:网站首页>[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;
}

边栏推荐
- How to open the port number of the server, and the corresponding port of common network services
- Leetcode94 detailed explanation of middle order traversal of binary tree
- TCP triple handshake connection combing
- Why does TCP shake hands three times instead of two times (positive version)
- What does CRM mean? Three "key points" for CRM management software selection
- One year after I came to Ali, I ushered in my first job change
- Nuggets manufacturing industry, digital commerce cloud supply chain collaborative management system to achieve full chain intelligent management and control
- Tiktok's "online celebrity" was poached by Amazon and broadcast on Amazon live platform
- & 和 &&、| 和 || 的区别
- Tiktok shop will add a new site, and the Singapore site will be launched on June 9
猜你喜欢

Office fallback version, from 2021 to 2019

Super complete summary: how to operate files in go language

xtrabackup 实现mysql的全量备份与增量备份
![[translation] integration challenges in microservice architecture using grpc and rest](/img/88/53f4c2061b538b3201475ba51449ff.jpg)
[translation] integration challenges in microservice architecture using grpc and rest

Tiktok's "online celebrity" was poached by Amazon and broadcast on Amazon live platform

Taking advantage of the momentum, oceanbase promotes the lean growth of digital payment

Description of MATLAB functions

Unity解决Package Manager“You seem to be offline”

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

How to integrate and use log4net logging plug-in in vs2019 class library
随机推荐
LeetCode刷题系列-- 174. 地下城游戏
Leetcode94-二叉树的中序遍历详解
Super complete summary: how to operate files in go language
Pulse netizens have a go interview question, can you answer it correctly?
Learning transformer: overall architecture and Implementation
Office fallback version, from 2021 to 2019
Tiktok shop will add a new site, and the Singapore site will be launched on June 9
Nuggets manufacturing industry, digital commerce cloud supply chain collaborative management system to achieve full chain intelligent management and control
After watching the documentary "pirate treasure on Adak Island", I thought of the lost treasure in Chinese history
Configuration of uni app page.json title bar
Tiktok shop platform will take disciplinary measures against sellers who violate rules and policies
[example of URDF exercise based on ROS] use of four wheeled robot and camera
Developing ebpf program with go language
Unity solves the package manager "you see to be offline"
What is tiktok creator fund and how to withdraw it?
Tang Yudi opencv background modeling
[FFH] openharmony gnawing paper growth plan -- Application of cjson in traditional c/s model
Tiktok's "online celebrity" was poached by Amazon and broadcast on Amazon live platform
Tongxin UOS developer account has supported uploading the HAP package format of Hongmeng application
在npm上发布自己的库