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,31,3,2
3,2,11,2,3
1,1,51,5,1

题目简要:题目涉及到的是全排列问题,全排列的方法有很多,每种全排列的方法得到的排列顺序不同,但是对于一种全排列顺序而言,下一个排列指的是我们所用的排列方法得到一个排列顺序,指定一个排列,它在得到的排列顺序中的下一个排列是什么?本题目是按字典法进行的。

如1,2,3进行全排列为:

1,2,3

1,3,2

2,1,3

2,3,1

3,1,2

3,2,1

这样我们指定一个排列为1,2,3,那么它的下一个排列就是1,3,2.

具体做法:以5,4,7,5,3,2为例,

  5,4,7,5,3,2是一个排列,并且我们知道一个排列是有规律的,一个排列是有规律的,一个排列可以分为两个部分(其中一个部分可以为空),其中部分一定是递减顺序的,而另一个也是递减顺序的。5,4是递减的,7,5,4,3是递增的。

  要想得到下一个排列,首先我们必须知道将要变动的值是那个。全排的最后一个序列一定是递减顺序的,所以排列两部分中递减顺序的哪部分一定不需要改变,所以我们首先要找到id一个非递减顺序的值,也就是4位置为1,然后找到要和4交换的值,这个值是要在4之后的那个值中查找,要求是大于4的最小的那个,又因为我们是从右向做的查找,所以第一个大于4的即可。交换位置后,对位置1后面的序列进行排序。排序的原因是由字典排序的特性,如1,2,3时,当第一值变为2的时候为2,1,3, 2 后面的序列一定是从小到大的排序的,所以当我们交换两个值后要进行排序。又因为这个算法不看排序方法的时候时间复杂度为O(n),所以这个算法的时间复杂度取决于你的排序算法的时间复杂度。

void Sort(int* arr,int low ,int high)
{
if(low>=high)return ;
int val=arr[low];
int i=low;
int j=high;
while(i<j){
while(i<j&&arr[j]>val)j--;
arr[i]=arr[j];
while(i<j&&arr[i]<=val)i++;
arr[j]=arr[i];
}
arr[i]=val;
Sort(arr,low,i-);
Sort(arr,i+,high);
}
void nextPermutation(int* nums, int numsSize) {
if(numsSize<=)return ;
int i=numsSize-;
while(i>)
{
if(nums[i]>nums[i-])break;
i--;
}
printf("%d\n",nums[i-]);
if(i<=)Sort(nums,,numsSize-);
else{ for(int j=numsSize-;j>=i;j--)
if(nums[i-]<nums[j]){
nums[i-]+=nums[j];
nums[j]=nums[i-]-nums[j];
nums[i-]-=nums[j];
Sort(nums,i,numsSize-);
break ;
}
}
}

  

Next Permutation的更多相关文章

  1. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  2. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  4. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  7. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  8. Permutation test: p, CI, CI of P 置换检验相关统计量的计算

    For research purpose, I've read a lot materials on permutation test issue. Here is a summary. Should ...

  9. Permutation

    (M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II

随机推荐

  1. ctags and vim

    1,源码目录下第归检索. ctags -R * 2,搜索tag并用vim打开: vim -t <tag> 3,在vim 下的一些操作: Keyboard command Action Ct ...

  2. 对于.NET Socket连接的细节记录

    如果客户端直接连接一个不存在的服务器端,客户端会抛出异常: 如果在连接过程中,客户端强制关闭了连接(没有调用Close直接关闭了程序),服务器端会抛出异常: 如果在连接过程中,客户端调用了Close, ...

  3. mysql命令行以及mysql workbence查询结果中文乱码的解决方法

    最近正在学习mysql,安装环境是windows server 2003 32位操作系统+mysql 5.1.47同时也安装了mysql命令行以及mysql workbench这里是test数据库cr ...

  4. java通用的方法整理

    判断字符串是否为空 public static boolean IsEmpty(String str){ if (str == null){ return true; } if ("&quo ...

  5. 对vector<int>进行快速排序

    #include <iostream>#include <string>#include <vector>using namespace std;void Quic ...

  6. 数据类型int、bigint、smallint 和 tinyint范围

      bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字).存储大小为 8 个字节. int ...

  7. input标签name与value区别

    id是唯一标识符,不允许有重复值(类似数据表的主键)可以通过它的值来获得对应的html标签对象.(如果在同一页面代码中,出现重复的id,会导致不可预料的错误) name:单独地在一个网页里面,一个控件 ...

  8. JS第一天基础总结

    今天开始学习javascript 主要讲理论上的东西,例如,什么是脚本语言,什么是变量,什么是DOM,什么是BOM等. 脚本语言,实质上可以理解为我们编写的剧本,浏览器按照我们编写的剧本一步一步完成剧 ...

  9. AJAX实现异步登录

    //代码较为简单,只是测试所用 1.html登录页面代码 <table> <tr> <td>用户名:</td> <td><input ...

  10. JS小总结

    今天看了一下js的部分总结了一下 函数的定义首先是命名函数function funcName函数还有匿名函数 var funcName = function()普通函数和匿名函数在调用上基本没有什么区 ...