LeetCode: Next Permutation 解题报告
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,23,2,1 → 1,2,31,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 解题报告的更多相关文章
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 【LeetCode】31. Next Permutation 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...
- 【LeetCode】266. Palindrome Permutation 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
- LeetCode 784 Letter Case Permutation 解题报告
题目要求 Given a string S, we can transform every letter individually to be lowercase or uppercase to cr ...
- codeforces 500B.New Year Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...
随机推荐
- 洛谷 p1434 滑雪【记忆化搜索】
<题目链接> Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- Mysql学习(一)添加一个新的用户并用golang操作Mysql
Mysql添加一个新的用户并赋予权限 添加一个自己的用户到mysql 首先我们需要先用root用户登录mysql,但是刚安装完没有密码,我们先跳过密码 ailumiyana@ailumiyana:~/ ...
- iOS 技术篇: 如何利用dsym文件分析苹果被拒日志
今天提审被拒了.伤心
- 利用FutureTask进行超时设置方法
public class Test { public static void main(String[] args) { ExecutorService executor = Executors. ...
- BZOJ.5417.[NOI2018]你的名字(后缀自动机 线段树合并)
LOJ 洛谷 BZOJ 考虑\(l=1,r=|S|\)的情况: 对\(S\)串建SAM,\(T\)在上面匹配,可以得到每个位置\(i\)的后缀的最长匹配长度\(mx[i]\). 因为要去重,对\(T\ ...
- Python3基础系列-基本入门语法
本文简单地介绍了python的一些基本入门知识,通过对这些知识的了解,大家可以写一些简单的代码,同时也为后面深入理解打下基础.本文的主要内容如下: 值和类型 值,即value,通常有:1,2,3.14 ...
- Python3练习题系列(01)
2018-06-13 题目: 根据用户回答做出相应的判断,完成一个“回答-判断”的小游戏 Python3知识点: if, else, elif 实例代码: print("You enter ...
- Minor GC 与Full GC有什么不一样
新生代GC(Minor GC):指发生在新生代的垃圾收集动作,因为java对象大多都具备朝生夕灭的特性,所以Minor GC非常频繁,一般回收速度也非常快 老年代GC(Major GC/Full GC ...
- javac编译出现需要标识符问题解决
因为没有写public static void mian(String[] args) 在类里面只有属性和方法,内部类.不能直接写System.out.println():
- Vs2017 控制台 中文输出是乱码的问题解决
下午直接用vs写的控制台的东西,然后发现控制台输出的中文是乱码,于是就百度了下.同样的是,百度上很多的答案.我就说下我解决的过程.先上图 第一种方案:有可能是控制台的问题.若是控制台的问题,则与VS无 ...