当前位置:网站首页>【Hot100】31. 下一个排列
【Hot100】31. 下一个排列
2022-07-04 16:03:00 【王六六的IT日常】
中等题
整数数组的 下一个排列 是指其整数的下一个字典序更大的排列。
解题思路:
- 找到第一个比后面小的元素
- 与后面比它大的最小值交换
- 交换后使他后面的数字升序排列, reverse即可, 因为 1. 的特性,后面是降序排列的
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); // 边界值处理
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;
}
}
}
边栏推荐
- 7 RSA Cryptosystem
- [Huawei HCIA continuous update] SDN and FVC
- 超标量处理器设计 姚永斌 第6章 指令解码 摘录
- Vscode modification indentation failed, indent four spaces as soon as it is saved
- Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection
- 中断的顶半部和底半部介绍以及实现方式(tasklet 和 工作队列)
- ble HCI 流控机制
- Hidden corners of coder Edition: five things that developers hate most
- Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
- 《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
猜你喜欢
Zebras are recognized as dogs, and the reason for AI's mistakes is found by Stanford
[Huawei HCIA continuous update] SDN and FVC
Master the use of auto analyze in data warehouse
Go micro tutorial - Chapter 2 go micro V3 using gin and etcd
wuzhicms代码审计
超标量处理器设计 姚永斌 第5章 指令集体系 摘录
Datakit -- the real unified observability agent
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
Implementation of super large-scale warehouse clusters in large commercial banks
雨量预警广播自动化数据平台BWII 型广播预警监测仪
随机推荐
Zebras are recognized as dogs, and the reason for AI's mistakes is found by Stanford
【HCIA持续更新】WLAN工作流程概述
7 RSA密码体制
整理混乱的头文件,我用include what you use
Redis 的内存淘汰策略和过期删除策略的区别
Load test practice of pingcode performance test
整理混乱的头文件,我用include what you use
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
码农版隐秘的角落:作为开发者最讨厌的5件
What is low code development?
New technology releases a small program UNIPRO to meet customers' mobile office scenarios
tx.origin安全问题总结
利用win10计划任务程序定时自动运行jar包
OPPO小布推出预训练大模型OBERT,晋升KgCLUE榜首
Pytorch deep learning quick start tutorial
Datakit -- the real unified observability agent
leetcode:421. The maximum XOR value of two numbers in the array
Analysis of abnormal frequency of minor GC in container environment
简单易用的地图可视化
长城证券安全不 证券开户