当前位置:网站首页>【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;
}
}
}
边栏推荐
- Learn more about the basic situation of 2022pmp examination
- I2C子系统之适配器的设备接口分析(i2c-dev.c文件分析)
- 【华为HCIA持续更新】SDN与FVC
- [test development] software testing - Basics
- 什么是低代码开发?
- 内核中时间相关的知识介绍
- 《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
- 整理混乱的头文件,我用include what you use
- Offline and open source version of notation -- comprehensive evaluation of note taking software anytype
- Master the use of auto analyze in data warehouse
猜你喜欢
![[Huawei HCIA continuous update] SDN and FVC](/img/02/f86b509fdc515f14a4497090f0d070.png)
[Huawei HCIA continuous update] SDN and FVC

【HCIA持续更新】网络管理与运维
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下

Analysis of abnormal frequency of minor GC in container environment

超标量处理器设计 姚永斌 第6章 指令解码 摘录

一加10 Pro和iPhone 13怎么选?

Learn more about the basic situation of 2022pmp examination

整理混乱的头文件,我用include what you use

【HCIA持续更新】广域网技术

Hidden corners of coder Edition: five things that developers hate most
随机推荐
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
一文掌握数仓中auto analyze的使用
【HCIA持续更新】网络管理与运维
【Proteus仿真】基于VSM 串口printf调试输出示例
KS007基于JSP实现人个人博客系统
Ks007 realizes personal blog system based on JSP
长城证券安全不 证券开户
防火墙基础透明模式部署和双机热备
What is low code development?
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
【HCIA持续更新】广域网技术
Dynamic programming stock problem comparison
聊聊异步编程的 7 种实现方式
Vb无法访问数据库stocks
国产数据库TiDB初体验:简单易用,快速上手
VSCode修改缩进不成功,一保存就缩进四个空格
整理混乱的头文件,我用include what you use
补能的争议路线:快充会走向大一统吗?
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
高中物理:力、物体和平衡