当前位置:网站首页>【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;
}
}
}
边栏推荐
- Is it safe for Bank of China Securities to open an account online?
- 智捷云——元宇宙综合解决方案服务商
- [unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
- Wuzhicms code audit
- Win32 API 访问路由的加密网页
- DataKit——真正的统一可观测性 Agent
- Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
- Web game engine
- [HCIA continuous update] overview of WLAN workflow
- 居家打工年入800多万,一共五份全职工作,他还有时间打游戏
猜你喜欢
居家打工年入800多万,一共五份全职工作,他还有时间打游戏
leetcode:421. The maximum XOR value of two numbers in the array
【HCIA持续更新】网络管理与运维
整理混乱的头文件,我用include what you use
【HCIA持续更新】WLAN工作流程概述
Zhijieyun - meta universe comprehensive solution service provider
Kunming Third Ring Road Closure project will pass through these places. Is there one near your home?
[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
Internet addiction changes brain structure: language function is affected, making people unable to speak neatly
电子宠物小狗-内部结构是什么?
随机推荐
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
无心剑中译伊丽莎白·毕肖普《一门技艺》
Kunming Third Ring Road Closure project will pass through these places. Is there one near your home?
About the pit of firewall opening 8848 when Nacos is started
【HCIA持续更新】WLAN工作流程概述
PingCode 性能测试之负载测试实践
国产数据库TiDB初体验:简单易用,快速上手
Implementation of super large-scale warehouse clusters in large commercial banks
7 RSA密码体制
防火墙基础透明模式部署和双机热备
What are cache penetration, cache breakdown, and cache avalanche
如何进行MDM的产品测试
电子宠物小狗-内部结构是什么?
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection
DataKit——真正的统一可观测性 Agent
Datakit -- the real unified observability agent
How to choose one plus 10 pro and iPhone 13?
detectron2安装方法
【华为HCIA持续更新】SDN与FVC