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.紧接着是一个 ...
随机推荐
- hdu 2844 Coins【多重背包】
题目链接:https://vjudge.net/contest/228640#problem/F 转载于:http://www.voidcn.com/article/p-mxcorksq-gh.htm ...
- 不一样的go语言-gopher
前言 gopher原意地鼠,在golang 的世界里解释为地道的go程序员.在其他语言的世界里也有PHPer,Pythonic的说法,反而Java是个例外.虽然也有Javaer之类的说法,但似乎并 ...
- 玩转SpringCloud(F版本) 三.断路器(Hystrix)RestTemplate+Ribbon和Feign两种方式
此文章基于: 玩转SpringCloud 一.服务的注册与发现(Eureka) 玩转SpringCloud 二.服务消费者(1)ribbon+restTemplate 转SpringCloud 二.服 ...
- mfc调用cmd执行完保留黑框
mfc调用cmd的方法有很多,本文采用 ShellExecute ShellExecute(AfxGetMainWnd()->m_hWnd,L"open",L"cm ...
- Microsoft Corporation 去掉 windows 修改 启动加载 版权
windows 修改 开机界面 boot启动界面 windows 修改 启动加载 版权 windows 系统如何修改开机画面的版权文字“Microsoft Corporation ... ◎Micro ...
- loj#2128. 「HAOI2015」数字串拆分 矩阵乘法
目录 题目链接 题解 代码 题目链接 loj#2128. 「HAOI2015」数字串拆分 题解 \(f(s)\)对于\(f(i) = \sum_{j = i - m}^{i - 1}f(j)\) 这个 ...
- Codeforces.1045A.Last chance(最大流ISAP 线段树优化建图)
题目链接 \(Description\) 你需要用给定的\(n\)个武器摧毁\(m\)架飞船中的某一些.每架飞船需要被摧毁恰好一次. 武器共三种:1.可以在给定的集合中摧毁一架飞船:2.可以摧毁区间\ ...
- POJ.3145.Common Substrings(后缀数组 倍增 单调栈)
题目链接 \(Description\) 求两个字符串长度不小于k的公共子串对数. \(Solution\) 求出ht[]后先减去k,这样对于两个后缀A',B',它们之间的贡献为min{ht(A)}( ...
- Python3基础之基本问题
问题1: 加法运算符重载 如果我们有两个列表对象,我要将两个列表中的元素依下标进行加和,我们该如何实现? 1列表对象的加法 list1 = [1,2,3,4] list2 = [10,20,30,40 ...
- java中动态给sql追加?号
/* * 用来生成where子句 len数组的长度 */ private String toWhereSql(int len) { StringBuilder sb = new StringBuild ...