当前位置:网站首页>【Hot100】31. Next spread
【Hot100】31. Next spread
2022-07-04 17:47:00 【Wang Liuliu's it daily】
Medium question
Integer array Next spread It refers to the next lexicographic order of its integers .
Their thinking :
- Find the first element smaller than the following
- Exchange with the following minimum value larger than it
- After the exchange, the numbers behind him are arranged in ascending order , reverse that will do , because 1. Characteristics of , The following is in descending order
class Solution {
public void nextPermutation(int[] nums) {
int pre = nums.length - 2;
while (pre >= 0 && nums[pre] >= nums[pre + 1]) pre--;
if (pre == -1) reverse(nums, 0, nums.length - 1); // Boundary value processing
else {
int post = pre + 1;
while (post < nums.length && nums[post] > nums[pre]) post++;
post--;
int t = nums[pre];
nums[pre] = nums[post];
nums[post] = t;
reverse(nums, pre + 1, nums.length - 1);
}
}
void reverse(int[] nums, int l, int r) {
while (l < r) {
int t = nums[l];
nums[l++] = nums[r];
nums[r--] = t;
}
}
}
边栏推荐
- High school physics: force, object and balance
- I2C子系统之适配器的设备接口分析(i2c-dev.c文件分析)
- Using win10 scheduling task program to automatically run jar package at fixed time
- VB cannot access database stocks
- Master the use of auto analyze in data warehouse
- [proteus simulation] printf debugging output example based on VSM serial port
- Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
- 无心剑中译伊丽莎白·毕肖普《一门技艺》
- 【HCIA持续更新】WLAN概述与基本概念
- Display opencv drawn pictures on MFC picture control control
猜你喜欢
【HCIA持续更新】广域网技术
kaili不能输入中文怎么办???
居家打工年入800多万,一共五份全职工作,他还有时间打游戏
什么是低代码开发?
To sort out messy header files, I use include what you use
[HCIA continuous update] WLAN overview and basic concepts
Using win10 scheduling task program to automatically run jar package at fixed time
La 18e Conférence internationale de l'IET sur le transport d'électricité en courant alternatif et en courant continu (acdc2022) s'est tenue avec succès en ligne.
OPPO小布推出预训练大模型OBERT,晋升KgCLUE榜首
RecastNavigation 之 Recast
随机推荐
Ks007 realizes personal blog system based on JSP
[proteus simulation] printf debugging output example based on VSM serial port
I2C子系统之适配器的设备接口分析(i2c-dev.c文件分析)
长城证券安全不 证券开户
简单易用的地图可视化
Vb无法访问数据库stocks
Win32 API 访问路由的加密网页
Two methods of MD5 encryption
The top half and bottom half of the interrupt are introduced and the implementation method (tasklet and work queue)
【HCIA持续更新】广域网技术
【华为HCIA持续更新】SDN与FVC
创业两年,一家小VC的自我反思
明星开店,退,退,退
Set the transparent hidden taskbar and full screen display of the form
超标量处理器设计 姚永斌 第6章 指令解码 摘录
Initial experience of domestic database tidb: simple and easy to use, quick to start
你应该懂些CI/CD
高中物理:力、物体和平衡
开发者,MySQL专栏完更,助你轻松从安装到入门进阶
Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see