[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 ...
随机推荐
- 赢在面试之Java泛型篇(十二)
139. Java中的泛型是什么 ? 使用泛型的好处是什么? 泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数. 好处: 1.类型安全,提供编译期 ...
- cardview和Palette,ActionBar颜色随图改变
CardView是一个控件,Palette是取色工具(工具类),本文会对他们进行比较细致的介绍,相信机制的各位看完一定轻而易举地实现ActionBar随图改变的特效. 首先看一下效果图: Gradle ...
- css一些简单的例子
1.http协议 一:HTTP协议:hypertext transport protocol(超文本传输协议) 特点: 1.请求与响应 2.无状态的协议 请求协议: 请求首行: 请求头信息: Acce ...
- Oracle11g静默安装
静默安装Oracle11G 前期准备 1虚拟机准备: Centos 6.5 64位 内存2G 硬盘30G Cpu 4核 Hostname silent 将安装包放到/tmp/oracle目录下 2.软 ...
- sklearn.neighbors.kneighbors_graph的简单属性介绍
connectivity = kneighbors_graph(data, n_neighbors=7, mode='distance', metric='minkowski', p=2, inclu ...
- git log 中文乱码问题(浪费了一天)
git log和gitcommit中文出现乱码,花了大半天的时间试了网上的各种方法,还是搞不定. 只好放大招. 卸载软件后重装,还没有进行任何配置,git config --list 发现有大量的配置 ...
- CCF-201403-1-相反数
问题描述 试题编号: 201403-1 试题名称: 相反数 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 有 N 个非零且各不相同的整数.请你编一个程序求出它们中有多少对相反 ...
- QT之UDP通信
前言:前一篇讲了TCP通信,这篇来看看UDP通信. 这里说明一下,UDP通信中分为三种通信分别为单播.组播和广播,下面将一一为大家介绍. 同样的我们都需要在工程文件中添加network QT += c ...
- windows 终端命令详解
打开"运行"对话框(Win+R),输入cmd,打开控制台命令窗口... 也可以通过cmd /c 命令 和 cmd /k 命令的方式来直接运行命令 注:/c表示执行完命令后关闭cmd ...
- Java 模拟栈结构
栈和队列: 通常是作为程序猿的工具,用于辅助构思算法.生命周期较短,执行时才被创建 訪问受限.在特定时刻,仅仅有一个数据可被读取或删除 是一种抽象的结构.内部的实现机制.对用户不可见.比方用数组.链表 ...