当前位置:网站首页>【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;
}
}
}
边栏推荐
- Hidden corners of coder Edition: five things that developers hate most
- Is it safe for CITIC Securities to open an account online? Is the account opening fee charged
- 新享科技发布小程序UniPro小优 满足客户移动办公场景
- [system analyst's road] Chapter 7 double disk system design (structured development method)
- 【Hot100】32. 最长有效括号
- 图像检索(image retrieval)
- 【系统分析师之路】第七章 复盘系统设计(结构化开发方法)
- S5PV210芯片I2C适配器驱动分析(i2c-s3c2410.c)
- MVC mode and three-tier architecture
- Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
猜你喜欢

leetcode:421. The maximum XOR value of two numbers in the array

创业两年,一家小VC的自我反思

Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see

【Hot100】32. 最长有效括号

使用3DMAX制作一枚手雷

With an annual income of more than 8 million, he has five full-time jobs. He still has time to play games

PingCode 性能测试之负载测试实践

Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection

一加10 Pro和iPhone 13怎么选?

Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
随机推荐
Face_recognition人脸识别之考勤统计
I2C子系统之适配器的设备接口分析(i2c-dev.c文件分析)
What if Kaili can't input Chinese???
Easy to use map visualization
使用3DMAX制作一枚手雷
Device interface analysis of the adapter of I2C subsystem (I2C dev.c file analysis)
CocosCreator事件派发使用
The top half and bottom half of the interrupt are introduced and the implementation method (tasklet and work queue)
【测试开发】软件测试——基础篇
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
解读数据安全治理能力评估框架2.0,第四批DSG评估征集中
Web game engine
创业两年,一家小VC的自我反思
Detectron2 installation method
NFT流动性市场安全问题频发—NFT交易平台Quixotic被黑事件分析
关于nacos启动时防火墙开启8848的坑
上网成瘾改变大脑结构:语言功能受影响,让人话都说不利索
Wuzhicms code audit
【HCIA持续更新】网络管理与运维
Zhijieyun - meta universe comprehensive solution service provider