[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 ...
随机推荐
- mysql数据库相关知识
什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库.(来自:百度) 什么是sql? 结构化查询语言(Struct ...
- python学习笔记 函数
形式: def function(a,b,c=0,*args,**kw)#a,b必选参数,*args可变参数,**kw关键字参数 1.函数的返回值可以是多个参数.多个参数时,实际上返回的是一个tupl ...
- Axios 执行post发送两次请求的小坑
vue-resource2.0已经不再更新,所以vue2.0官方推荐使用axios来代替.实际项目也是应用上了vue+axios,然后就有了这么一段填坑的经历. 问题:axios使用post请求时,发 ...
- php执行linux命令的6个函数
一般情况下,很少会用php去执行linux命令,不过特殊情况下,你也许会用到这些函数.以前我知道有二个函数可以执行linux命令,一个是exec,一个是shell_exec.其实有很多的,结合手册内容 ...
- SpringData 基于SpringBoot快速入门
SpringData 基于SpringBoot快速入门 本章通过学习SpringData 和SpringBoot 相关知识将面向服务架构(SOA)的单点登录系统(SSO)需要的代码实现.这样可以从实战 ...
- js 匹配2个字符串相似度
strSimilarity2Number: function (s, t) { var n = s.length, m = t.length, d = []; var i, j, s_i, t_j, ...
- NavMesh--导航网格寻路
一.概述: NavMesh是3D游戏世界中用于实现动态物体自动寻路的一种技术,他将游戏场景中复杂的结构组织关系简化为带有一定信息的网格, 进而在这些网格的基础上通过一些列的计算来实现自动寻路. 二.简 ...
- webrtc视频数据接收端处理流程详解
- python re 正则匹配 split sub
import re 编译: motif='([ST])Q' seq="SQAAAATQ" regrex=re.compile(motif) #编译成正则对象 regrex=re.c ...
- mysql数据库插入数据获取自增主键的三种方式(jdbc PreparedStatement方式、mybatis useGeneratedKeys方式、mybatis selectKey方式)
通常来说对于mysql数据库插入数据获取主键的方法是采用selectKey的方式,特别是当你持久层使用mybatis框架的时候. 本文除此之外介绍其它两种获取主键的方式. 为了方便描述我们先建一张my ...