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. 解决linux下“XX不在 sudoers 文件中。此事将被报告"的问题

    在使用sudo命令时,经常性会提示出“不在 sudoers 文件中.此事将被报告”的错误信息. 这是因为当前登录的账号不在sudo权限里面. sudo命令可以让你以root身份执行命令,来完成一些我们 ...

  2. codeforces 854C.Planning 【贪心/优先队列】

    Planning time limit per test 1 second memory limit per test 512 megabytes input standard input outpu ...

  3. POJ1962:Corporative Network【带权并查集】

    <题目链接> 题目大意: n个节点,若干次询问,I x y表示从x连一条边到y,权值为|x-y|%1000:E x表示询问x到x所指向的终点的距离.   解题分析: 与普通的带权并查集类似 ...

  4. 洛谷 P2440 木材加工【基础二分】

    题目链接:https://www.luogu.org/problemnew/show/P2440 题目描述 木材厂有一些原木,现在想把这些木头切割成一些长度相同的小段木头(木头有可能有 剩余),需要得 ...

  5. jupyter notebook connecting to kernel problem

    前几天帮同学配置 python 和 anaconda 环境,在装 jupyter notebook 时,出了点问题,搞了一天半终于搞好了,也是在 github 里找到了这个问题的解答. 当时显示的是无 ...

  6. loj#2665. 「NOI2013」树的计数

    目录 题目链接 题解 代码 题目链接 loj#2665. 「NOI2013」树的计数 题解 求树高的期望 对bfs序分层 考虑同时符合dfs和bfs序的树满足什么条件 第一个点要强制分层 对于bfs序 ...

  7. POJ.2750.Potted Flower(线段树 最大环状子段和)

    题目链接 /* 13904K 532ms 最大 环状 子段和有两种情况,比如对于a1,a2,a3,a4,a5 一是两个端点都取,如a4,a5,a1,a2,那就是所有数的和减去不选的,即可以计算总和减最 ...

  8. Python面向对象——多态

    多态的作用 调用不同的子类将会产生不同的行为. 多态是在继承上实现的. 图解多态1 图解多态2 图解多态1代码 class AudioFile: def __init__(self, filename ...

  9. C++学习笔记42:进程管理

    子进程异步清除 SIGCHLD信号:子进程终止时,向父进程自动发送,编写此信号处理例程,异步清除子进程 #include <signal.h> #include <string.h& ...

  10. Git-TortoiseGit的安装和配置

    1.TortoiseGit只是一个程序壳,必须依赖一个 Git Core,也就是我们安装的 Git 下载:https://tortoisegit.org/download/ 先安装程序包,再安装语言包 ...