当前位置:网站首页>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
边栏推荐
- 48. 旋转图像
- 快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
- Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
- ClickHouse-SQL 操作
- Use of shell awk command
- 239. 滑动窗口最大值(困难)-单向队列、大顶堆-字节跳动高频题
- PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet
- Compare the performance of query based on the number of paging data that meet the query conditions
- 958. Complete binary tree test
- Perception of linear algebra 2
猜你喜欢
为什么有些人说单片机简单,我学起来这么吃力?
1217_ Generating target files using scons
基于51单片机红外无线通讯仿真
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
Learning record of uni app dark horse yougou project (Part 2)
[ES6] promise related (event loop, macro / micro task, promise, await / await)
Summary of common SQL statements
ClickHouse-表引擎
RPC核心概念理解
440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题
随机推荐
node中,如何手动实现触发垃圾回收机制
快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
Router object, route object, declarative navigation, programmed navigation
1217_ Generating target files using scons
Websocket (basic)
Use of todesk remote control software
209. Minimum length subarray - sliding window
ClickHouse-表引擎
练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
Devexpress GridView add select all columns
[WPF binding 3] listview basic binding and data template binding
基于51单片机红外无线通讯仿真
01-初识sketch-sketch优势
The system cannot be started after AHCI is enabled
Kubernetes 服务发现 监控Endpoints
双指针进阶--leetcode题目--盛最多水的容器
Excel quickly and automatically fills the contents of a row on a blank cell
How to use the input table one-way service to send (occupy less) picture files (body transmission)? FileReader built-in object involved
440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题
Clickhouse - data type