当前位置:网站首页>31. Next arrangement
31. Next arrangement
2022-04-23 17:39:00 【hequnwang10】
One 、 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 .
Example 1:
Input :nums = [1,2,3]
Output :[1,3,2]
Example 2:
Input :nums = [3,2,1]
Output :[1,2,3]
Example 3:
Input :nums = [1,1,5]
Output :[1,5,1]
Two 、 Problem solving
Two traversal
class Solution {
public void nextPermutation(int[] nums) {
// You need to find the first ascending subscript from the back
int length = nums.length;
int end = length-2;
// If the current value is greater than the following value , Keep looking for , Always find the first ascending subscript
while(end >= 0 && nums[end] >= nums[end+1]){
end--;
}
// After finding the subscript value , Find a value greater than the subscript value after
// Pay attention to the boundary conditions
if(end >= 0){
int next = length - 1;
while(next >= 0 && nums[end] >= nums[next]){
next--;
}
swap(nums,end,next);
}
reverse(nums,end+1);
}
public void swap(int[] nums,int left,int right){
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
public void reverse(int[] nums,int left){
int right = nums.length-1;
while(left <= right){
swap(nums,left,right);
left++;
right--;
}
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231732009984.html
边栏推荐
- 快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
- MySQL进阶学习之SQL优化【插入,主键,排序,分组,分页,计数】
- JVM类加载机制
- Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
- Advantages and disadvantages of several note taking software
- [ES6] promise related (event loop, macro / micro task, promise, await / await)
- stm32入门开发板选野火还是正点原子呢?
- Shell-入门、变量、以及基本的语法
- Read software engineering at Google (15)
- How does matlab draw the curve of known formula and how does excel draw the function curve image?
猜你喜欢
[difference between Oracle and MySQL]
C语言函数详解
Summary of common SQL statements
Halo open source project learning (II): entity classes and data tables
Use of todesk remote control software
stm32入门开发板选野火还是正点原子呢?
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
2021长城杯WP
练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
HCIP第五次实验
随机推荐
嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
Qt 修改UI没有生效
Excel quickly and automatically fills the contents of a row on a blank cell
122. The best time to buy and sell stocks II - one-time traversal
双闭环直流调速系统matlab/simulink仿真
Open futures, open an account, cloud security or trust the software of futures companies?
JS failed to change all variables and changed to the return method. Finally, the problem was solved
ASP. Net core dependency injection service life cycle
209. Minimum length subarray - sliding window
ClickHouse-数据类型
uni-app黑马优购项目学习记录(下)
Seven cattle upload pictures (foreground JS + background C API get token)
102. 二叉树的层序遍历
[difference between Oracle and MySQL]
The system cannot be started after AHCI is enabled
Manually implement call, apply and bind functions
Learning record of uni app dark horse yougou project (Part 2)
Ring back to origin problem - byte jumping high frequency problem
JVM类加载机制
Shell-sort命令的使用