当前位置:网站首页>【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;
}
}
}
边栏推荐
- Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
- About the pit of firewall opening 8848 when Nacos is started
- New technology releases a small program UNIPRO to meet customers' mobile office scenarios
- To sort out messy header files, I use include what you use
- VSCode修改缩进不成功,一保存就缩进四个空格
- 开发者,MySQL专栏完更,助你轻松从安装到入门进阶
- 【模板】【luogu P4630】Duathlon 铁人两项(圆方树)
- Load test practice of pingcode performance test
- I2C子系统之适配器的设备接口分析(i2c-dev.c文件分析)
- What are cache penetration, cache breakdown, and cache avalanche
猜你喜欢

简单易用的地图可视化

就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题

Vscode modification indentation failed, indent four spaces as soon as it is saved

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

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

智捷云——元宇宙综合解决方案服务商

Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue

Firewall basic transparent mode deployment and dual machine hot standby

Implementation of super large-scale warehouse clusters in large commercial banks

wuzhicms代码审计
随机推荐
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
curl 命令妙用
【HCIA持续更新】WLAN概述与基本概念
[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
整理混乱的头文件,我用include what you use
Face_recognition人脸识别之考勤统计
Electronic pet dog - what is the internal structure?
Internet addiction changes brain structure: language function is affected, making people unable to speak neatly
缓存穿透、缓存击穿、缓存雪崩分别是什么
[HCIA continuous update] overview of WLAN workflow
S5PV210芯片I2C适配器驱动分析(i2c-s3c2410.c)
To sort out messy header files, I use include what you use
R语言plotly可视化:plotly可视化多分类变量小提琴图(multiple variable violin plot in R with plotly)
Zebras are recognized as dogs, and the reason for AI's mistakes is found by Stanford
就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题
shell脚本的替换功能实现
Leetcode list summary
leetcode刷题目录总结