Next Permutation

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

SOLUTION 1:

1.  From the tail to find the first digital which drop
  example: 12 4321

首先我们找到从尾部到头部第一个下降的digit. 在例子中是:2

2. 把从右到左第一个比dropindex大的元素换过来。

3. 把dropindex右边的的序列反序

4. 如果1步找不到这个index ,则不需要执行第二步。

 public class Solution {
public void nextPermutation(int[] num) {
if (num == null) {
return;
} int len = num.length; // Find the index which drop.
int dropIndex = -1;
for (int i = len - 1; i >= 0; i--) {
if (i != len - 1 && num[i] < num[i + 1]) {
dropIndex = i;
break;
}
} // replace the drop index.
if (dropIndex != -1) {
for (int i = len - 1; i >= 0; i--) {
if (num[i] > num[dropIndex]) {
swap(num, dropIndex, i);
break;
}
}
} // reverse the link.
int l = dropIndex + 1;
int r = len - 1;
while (l < r) {
swap(num, l, r);
l++;
r--;
}
} public void swap(int[] num, int l, int r) {
int tmp = num[l];
num[l] = num[r];
num[r] = tmp;
} }

SOLUTION 2:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/permutation/NextPermutation.java

LeetCode: Next Permutation 解题报告的更多相关文章

  1. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  2. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  3. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  4. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  5. 【LeetCode】31. Next Permutation 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...

  6. 【LeetCode】266. Palindrome Permutation 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  7. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...

  8. LeetCode 784 Letter Case Permutation 解题报告

    题目要求 Given a string S, we can transform every letter individually to be lowercase or uppercase to cr ...

  9. codeforces 500B.New Year Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...

随机推荐

  1. 用redis来实现Session保存的一个简单Demo

    现在很多项目都用Redis(RedisSessionStateProvider)来保存Session数据,但是最近遇到一个比较典型的情况,需要把用户数据全部load到redis里面,在加上RedisS ...

  2. 用Python破解斗地主残局

    相信大家都玩过斗地主,规则就不再介绍了. 直接上一张朋友圈看到的残局图: 这道题我刚看到时,曾尝试用手工来破解,每次都以为找到了农民的必胜策略时,最后都发现其实农民跑不掉.由于手工破解无法穷尽所有可能 ...

  3. python处理转义字符

    python2 #1. import HTMLParser HTMLParser.HTMLParser().unescape('Suzy & John') #2. from xml.sax.s ...

  4. json Map JsonObject JsonArray

    json字符串是不应包含 "\"转义字符的,json不能通过js json工具转换或者java json工具 包转换那么一定程度上json字符串已被在一次处理不在能转成json了. ...

  5. prufer序列计数的一些结论

    \(prufer\)序列和完全图的生成树一一对应(考虑构造) 完全图的生成树个数为\(n^{n - 2}\) 满足第\(i\)个点的度数为\(d_i\)的生成树为\(\frac{n!}{\prod ( ...

  6. 项目出现小红叉,类名上带有 Implicit错误

    Implicit super constructor Object() is undefined for default constructor. Must define an explicit co ...

  7. 3DMax 2014中文版安装破解教程

    周末的时候,因为帮忙别人做动画,要用到3dmax.然后发现自己真的很菜啊....弄了好久,然后终于阔以了,以后在慢慢研究.贴出详细的步骤: . 1.如果没有软件,就请自行下载[百度上很多的] 2.双击 ...

  8. MIRUO面试题

    1.c#可以继承string类吗?2.接口可以实现接口吗?抽象类可以实现接口吗?抽象类可以实现实体类吗?3.用C#计算2.5的3次方的方法.4.什么是协同程序?5.GC是什么,如何减少内存,如何加快性 ...

  9. Educational Codeforces Round 19 题解【ABCDE】

    A. k-Factorization 题意:给你一个n,问你这个数能否分割成k个大于1的数的乘积. 题解:因为n的取值范围很小,所以感觉dfs应该不会有很多种可能-- #include<bits ...

  10. DB2表不活动的处理方法

    DB2表不活动的处理方法(转载)首先查一下: db2 57016 SQLSTATE 57016: 因为表不活动,所以不能对其进行访问. 解决方法为:执行命令:reorg table XXX:即可. 参 ...