[array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium
descrition
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
解析
方法 1
暴力解决,O(n!) 的时间复杂度求出数组的全排列,然后返回比当前排列大的序列中最小的。
方法 2
时间复杂度:O(n);空间复杂度:O(1)
不失一般性,假设数组如图所示。我们从后往前遍历数组,比较相邻的两个数,直到出现前一个数小于后一个数时,循环停止。如图中 a[i-1] < a[i],循环的结果有以下两点说明:
- 在 a[i,...n-1] 是非递增有序(递减有序)的数组,因为 for 循环提前结束的条件是 a[i-1] < a[i],如果没有提前结束则说明,a[i-1] > a[i]。在这样的情况下,我们需要找到 a[i,...,n-1] 中比 a[i-1] 大的数中最小的那个,假设为 a[j]。将 a[j] 和 a[i-1] 交换,由数组的性质可得 a[i,...,n-1] 依然保持非递增(递减)有序,这时我们只需要将 a[i,...,n-1] 反转,即可得到下一个最小的排列。
- 如果循环没有提前结束,则说明每一次比较都是 a[i-1] > a[i],说明整个数组非递增(递减)有序。在这样的情况下直接将数组反转即可得到最小的排列。
具体实现的逻辑参看代码。
code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution{
public:
void nextPermutation(vector<int>& nums) {
if(nums.empty())
return;
int isplit = -1;
for(int i=nums.size()-1; i>0; i--){
if(nums[i-1] < nums[i]){
isplit = i-1;
break;
}
}
// if isplit == -1, which indicate nums is in descending
// otherwhise, [isplit+1, n-1] is descending
if(isplit == -1){
reverse(nums, 0, nums.size()-1);
}else{
// construct next permutation
// find the smallest one from the numbers which larger than nums[isplit]
int ismallest = nums.size()-1;
while(ismallest>isplit && nums[ismallest] <= nums[isplit])
ismallest--;
// nums[ismallest] > nums[isplit] >= nums[ismallest+1, ... , n-1]
// and nums[isplit+1, ismallest-1] >= nums[ismallest] >= nums[ismallest+1,..,n-1]
swap(nums[isplit], nums[ismallest]);
// nums[isplit+1, ... , n-1] still in descending
// so just reverse
reverse(nums, isplit+1, nums.size()-1);
}
}
void reverse(vector<int>& nums, int ileft, int iright){
while(ileft < iright){
swap(nums[ileft], nums[iright]);
ileft++;
iright--;
}
}
};
int main()
{
return 0;
}
[array] leetcode - 31. Next Permutation - Medium的更多相关文章
- LeetCode: 31. Next Permutation (Medium)
1. 原题链接 https://leetcode.com/problems/next-permutation/description/ 2. 题目要求 给出一个整型数组,让我们给出下一个排序情况.注意 ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- LeetCode - 31. Next Permutation
31. Next Permutation Problem's Link ---------------------------------------------------------------- ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- leetcode 31. Next Permutation (下一个排列,模拟,二分查找)
题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...
- LeetCode 31. Next Permutation【Medium】
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode 31. Next Permutation (下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- [LeetCode] 31. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- ConstraintLayout+radioGroup做一个tab.简单好用。
主页tab是必须会有的,各种实现也很多.各有千秋.但目标都是简单.可控.今天用ConstraintLayout+radioGroup做一个tab.简单性可控性都还可以.本文目的把ConstraintL ...
- 《java.util.concurrent 包源码阅读》22 Fork/Join框架的初体验
JDK7引入了Fork/Join框架,所谓Fork/Join框架,个人解释:Fork分解任务成独立的子任务,用多线程去执行这些子任务,Join合并子任务的结果.这样就能使用多线程的方式来执行一个任务. ...
- Less的嵌套规则
Less的嵌套规则 在使用标准CSS时,要为多层嵌套的元素定义样式,要么使用后代选择器从外到内的嵌套定义,要么给这个元素加上类名或 id 来定义.这样的写法虽然很好理解,但维护起来很不方便,因为无法清 ...
- [smartMenu.js] 一个基于jquery的实用的右键拓展菜单栏插件
正在为电子书阅读器添加精准易用的标记功能,其中一个方案是扩展阅读器界面的右键菜单栏,使得用户右键点击某个词.子句.段落的时候可以进行扩展操作. 右键菜单栏有很多基于jQuery的插件,其中灵活性比较强 ...
- 内网神器-Bettercap
安装bettercap 1 2 root@sch01ar:~# apt-get update root@sch01ar:~# apt-get install bettercap 安装完成后查看一下帮 ...
- MFC中小笔记(四)
12.编译透明化界面是出现 WS_EX_LAYERED AC_SRC_ALPHA ULW_ALPHA ULW_OPAQUE undeclared identifier ,搜索发现SDK版本过低. ...
- C#检测获取移动硬盘盘符
最近做一个小工具, C# 对 移动硬盘的检测, var arr = DriveInfo.GetDrives(); 得出的所有磁盘,发现对于移动硬盘,DriveType 不是 Removable 类型 ...
- 解决tomcat部署包错误
Context namespace element 'annotation-config' and its parser class [org.springframework.context.anno ...
- gitup问题的处理
在使用本地建立git仓库后,准备使用 因为之前已经登录了,所以没有给登录的命令行 1.git init 命令初始化仓库 2.git add . 命令将所有的文件传输到git仓库 ...
- 第四届河南省ACM 序号互换 进制转换
序号互换 时间限制: 1 Sec 内存限制: 128 MB 提交: 41 解决: 19 [提交][状态][讨论版] 题目描述 Dr.Kong设计了一个聪明的机器人卡多,卡多会对电子表格中的单元格坐 ...