当前位置:网站首页>Force button 31. Next arrangement -- double finger needling
Force button 31. Next arrangement -- double finger needling
2022-07-24 15:23:00 【That girl outside the ocean】
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 .
link :https://leetcode.cn/problems/next-permutation
Example :
analysis
[1 , 2 , 3] There are six ways of arrangement that can be formed
[1, 2 , 3] 、[1,3,2] 、[2,1,3] [2,3,1] [3,1,2] [3,2,1]
arr = [1,2,3] The next line up for is [1,3,2]
We can think of it as finding a number composed of elements in the array , In all combinations of numbers , It is composed of elements that are larger than the original array and are the closest to the original array .
123<132<213<231<312<321
for example arr = [ 1,2,4,3 ] The next order of is [1,3,2,4]
stay 1,2,3,4 The number that can be composed of these four numbers
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
For the solution of this problem , I use double pointer :
Define two pointers Respectively i and j
Both of them point to the last number of the array 
Both pointers traverse forward , Find the pointer i The corresponding value is larger than the pointer j Corresponding value hours , Exchange data 

Obviously 1243<1324<1342
So after the exchange, the number after the pair (i+1 To the last ) Perform an ascending process
Get data [1,3,2,4]
When the element is already the maximum , Back to the first , That is to sort the elements to get an array
Code
class Solution {
public void nextPermutation(int[] nums) {
for(int i=nums.length-1;i>=0;i--){
for(int j=nums.length-1;j>i;j--){
// Data exchange
if(nums[i]<nums[j]){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
// An array nums Of i+1 Item to nums.length Sort items in ascending order
Arrays.sort(nums,i+1,nums.length);
return;
}
}
}
// If two for Cycle down and don't find (nums[i]<nums[j]) The situation of , Sort the array in ascending order and return
Arrays.sort(nums);
}
}
The time complexity is O(n)
边栏推荐
- Leetcode-09 (next rank + happy number + full rank)
- matlab图像去雾技术GUI界面-全局平衡直方图
- 2022 RoboCom 世界机器人开发者大赛-本科组(省赛)-- 第三题 跑团机器人 (已完结)
- Google Earth Engine——使用MODIS数据进行逐月数据的过火(火灾)面积并导出
- 2022 robocom world robot developer competition - undergraduate group (provincial competition) rc-u4 strategy team (completed)
- PyTorch的自动求导
- Route planning method for UAV in unknown environment based on improved SAS algorithm
- ReentrantLock 可重入锁
- Istio1.12: installation and quick start
- Here comes the problem! Unplug the network cable for a few seconds and plug it back in. Does the original TCP connection still exist?
猜你喜欢

26.文件使用磁盘的代码实现

Conflict resolution of onblur and onchange

2022 robocom world robot developer competition - undergraduate group (provincial competition) -- question 2: intelligent medication assistant (finished)

PyTorch的自动求导

被攻击怎么解决?DDoS高防IP防护策略

2022 RoboCom 世界机器人开发者大赛-本科组(省赛)RC-u4 攻略分队 (已完结)

2022 RoboCom 世界机器人开发者大赛-本科组(省赛)---第一题 不要浪费金币 (已完结)

Outlook tutorial, how to create tasks and to DOS in outlook?

24.原生磁盘的使用

27.目录与文件系统
随机推荐
Activity Registration: how to quickly start the open source tapdata live data platform on a zero basis?
Kubectl_ Easy to use command line tool: Oh my Zsh_ Tips and tricks
Wildfire STM32 domineering, through the firmware library to achieve water light
Kali concise language transformation method (illustration)
Spark Learning Notes (III) -- basic knowledge of spark core
[quantitative test]
dataframe 分组后排序的前n行 nlargest argmax idmax tail !!!!
图的存储和遍历
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)-- 第二题 智能服药助手 (已完结)
Is it safe for Huatai Securities to open an account? I don't know how to operate it
Outlook tutorial, how to create tasks and to DOS in outlook?
Intelligent operation and maintenance scenario analysis: how to detect abnormal business system status through exception detection
【Bug解决】Win10安装pycocotools报错
(09) flask is OK if it has hands - cookies and sessions
Sword finger offer II 001. integer division
Android SQLite database practice
Learning rate adjustment strategy in deep learning (1)
JMeter - call the interface for uploading files or pictures
You are only one SQL statement away from the tdengine Developer Conference!
How do novices buy stocks for the first time? Which securities company is the best and safest to open an account