当前位置:网站首页>【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章:多元函数的微分学
- 【测试开发】软件测试——基础篇
- It's too convenient. You can complete the code release and approval by nailing it!
- R language plot visualization: plot visualization of multiple variable violin plot in R with plot
- NFT流动性市场安全问题频发—NFT交易平台Quixotic被黑事件分析
- R语言plotly可视化:plotly可视化互相重叠的直方图(historgram)、并在直方图的顶部边缘使用geom_rug函数添加边缘轴须图Marginal rug plots
- To sort out messy header files, I use include what you use
- MVC模式和三层架构
- [template] [Luogu p4630] duathlon Triathlon (round square tree)
猜你喜欢
智捷云——元宇宙综合解决方案服务商
Ks007 realizes personal blog system based on JSP
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
Vb无法访问数据库stocks
【HCIA持续更新】网络管理与运维
The 18th IET AC / DC transmission International Conference (acdc2022) was successfully held online
Electronic pet dog - what is the internal structure?
上网成瘾改变大脑结构:语言功能受影响,让人话都说不利索
Learn more about the basic situation of 2022pmp examination
Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection
随机推荐
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
7 RSA Cryptosystem
超标量处理器设计 姚永斌 第6章 指令解码 摘录
斑马识别成狗,AI犯错的原因被斯坦福找到了丨开源
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
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.
开发者,MySQL专栏完更,助你轻松从安装到入门进阶
图像检索(image retrieval)
一文掌握数仓中auto analyze的使用
About the pit of firewall opening 8848 when Nacos is started
【华为HCIA持续更新】SDN与FVC
curl 命令妙用
Difference between redis' memory obsolescence strategy and expiration deletion strategy
超大规模数仓集群在大型商业银行的落地实践
解读数据安全治理能力评估框架2.0,第四批DSG评估征集中
Implementation of super large-scale warehouse clusters in large commercial banks
Load test practice of pingcode performance test
NFT liquidity market security issues occur frequently - Analysis of the black incident of NFT trading platform quixotic
7 RSA密码体制