当前位置:网站首页>每日一题-下一个排列-0723
每日一题-下一个排列-0723
2022-08-05 05:17:00 【菜鸡程序媛】
题目
整数数组的一个 排列 就是将其所有成员以序列或线性顺序排列。
例如,arr = [1,2,3] ,以下这些都可以视作 arr 的排列:[1,2,3]、[1,3,2]、[3,1,2]、[2,3,1] 。
整数数组的 下一个排列 是指其整数的下一个字典序更大的排列。更正式地,如果数组的所有排列根据其字典顺序从小到大排列在一个容器中,那么数组的 下一个排列 就是在这个有序容器中排在它后面的那个排列。如果不存在下一个更大的排列,那么这个数组必须重排为字典序最小的排列(即,其元素按升序排列)。
例如,arr = [1,2,3] 的下一个排列是 [1,3,2] 。
类似地,arr = [2,3,1] 的下一个排列是 [3,1,2] 。
而 arr = [3,2,1] 的下一个排列是 [1,2,3] ,因为 [3,2,1] 不存在一个字典序更大的排列。
给你一个整数数组 nums ,找出 nums 的下一个排列。
必须 原地 修改,只允许使用额外常数空间。
思路
- 从右面开始找到第一对左面比右面小的组合,确定 j,nums[j] < nums[i]
- 如果这个时候 j>=0,证明存在上面这样的组合,这个时候赋值k=nums.length-1的下标,找到最右面第一个比nums[j]大的数字(因为队尾肯定是从j开始往后算最小的数字),与nums[j]进行交换;如果j<0,证明当前的数组是严格单调递减的,已经是最大的排列了,那么直接执行第三步
- 就是将j+1开始的下标,与后面的数字进行交换,实现单调递增,即越小的数字位数越高
代码
public void nextPermutation(int[] nums) {
if(nums == null || nums.length <= 0)
return;
// 找到第一对前面数字小于后面数字的
int i = nums.length - 1;
int j = nums.length - 2;
while((j >= 0) && (nums[j] >= nums[i])){
j --;
i --;
}
if(j >= 0 && nums[j] < nums[i]) {
int k = nums.length - 1;
while((k > j) && nums[k] <= nums[j]){
k --;
}
swap(nums, j, k);
}
reverse(nums, j + 1);
}
private void swap(int[] nums, int j, int k){
int temp = nums[j];
nums[j] = nums[k];
nums[k] = temp;
}
private void reverse(int[] nums, int index){
int left = index, right = nums.length - 1;
while(left < right){
swap(nums, left, right);
left ++;
right --;
}
}
边栏推荐
- 【ts】typescript高阶:键值类型及type与interface区别
- OSPF网络类型
- 【UiPath2022+C#】UiPath If条件语句
- SharedPreferences and SQlite database
- [Pytorch study notes] 11. Take a subset of the Dataset and shuffle the order of the Dataset (using Subset, random_split)
- LeetCode刷题之第55题
- LeetCode刷题之第746题
- 【UiPath2022+C#】UiPath 数据操作
- Detailed explanation of BroadCast Receiver (broadcast)
- LeetCode刷题之第416题
猜你喜欢
![[Kaggle project actual combat record] Steps and ideas sharing of a picture classification project - taking leaf classification as an example (using Pytorch)](/img/7d/7f1301c30034f1c247d41f04c40244.png)
[Kaggle project actual combat record] Steps and ideas sharing of a picture classification project - taking leaf classification as an example (using Pytorch)

A deep learning code base for Xiaobai, one line of code implements 30+ attention mechanisms.

leetCode刷题之第31题

LeetCode刷题之第24题
![[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)](/img/ac/884d8aba8b9d363e3b9ae6de33d5a4.png)
[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)

PID详解

SQL (2) - join window function view

发顶会顶刊论文,你应该这样写作

PoE视频监控解决方案
![[Database and SQL study notes] 8. Views in SQL](/img/22/82f91388f06ef4f9986bf1e90800f7.png)
[Database and SQL study notes] 8. Views in SQL
随机推荐
[Database and SQL study notes] 9. (T-SQL language) Define variables, advanced queries, process control (conditions, loops, etc.)
【shell编程】第二章:条件测试语句
LeetCode刷题之第54题
MaskDistill-不需要标注数据的语义分割
LeetCode刷题之第1024题
LeetCode刷题之第746题
【Shell编程】第一章:子串
【UiPath2022+C#】UiPath If条件语句
十、视图解析原理与源码分析
[Kaggle project actual combat record] Steps and ideas sharing of a picture classification project - taking leaf classification as an example (using Pytorch)
Redis设计与实现(第三部分):多机数据库的实现
十一、拦截器运行原理
面向小白的深度学习代码库,一行代码实现30+中attention机制。
ECCV2022 | RU&谷歌提出用CLIP进行zero-shot目标检测!
《基于机器视觉的输电线路交叉点在线测量方法及技术方案》论文笔记
手把手教你搭建小程序
一个小时教你如何掌握ts基础
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)
六、请求处理—获取请求参数系列注解是怎样工作的?
【ts】typescript高阶:条件类型与infer