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.紧接着是一个 ...
随机推荐
- 《Android进阶之光》--多线程编程
No1: 实现多线程的3中方法 1)继承Thread,重写run()方法 2)实现Runnable接口,并实现该接口的run()方法 3)实现Callable接口,重写call()方法 public ...
- 障碍路线Obstacle Course
P1649 [USACO07OCT]障碍路线Obstacle Course 裸的dfs,今天学了一个新招,就是在过程中进行最优性减枝. #include<bits/stdc++.h> us ...
- js时间戳转换时间、距当前时间
// 1.时间戳转化成时间格式 function getTime(times) { return new Date(parseInt(times) * 1000).toLocaleString().r ...
- 启动Azure模拟器出错解决方案
错误弹窗: 输出控制台: Microsoft Azure Tools: Warning: Overriding public port 80 to 2888 in role 'WebRole1'. M ...
- (华中科大)江南雨烟 C++ STL 专栏
本文转载来自,华中科技大学江南雨烟的C/C++专栏部分STL剖析文章,以作学习之用. [1] [C++ STL学习之一]容器的共通能力和共通操作总结 [2] [C++ STL学习之二]容器vect ...
- 随机查出满足条件的5条数据(tp5)
随机查出满足条件的5条数据 public function showQuestion() { $data[; $data[ $data['level'] = (int)$data['level']; ...
- Codeforces Round #109 (Div. 1) 题解 【ABC】
A - Hometask 题意:给你一个字符串,然后再给你k个禁止挨在一起的字符串,问你最少删除多少个字符串,使得不会有禁忌的字符串对挨在一起.题目保证每个字符最多出现在一个禁忌中. 题解:由于每个字 ...
- js获取本机id
var hostname = location.hostname; window.location.href="http://"+hostname+":8080/zhib ...
- Oracle ORA-12541:TNS:无监听程序
背景:自己机子做oracle服务器,其他机子可以ping得通我的机子,但是jdbc就是连不上,后来用plsql连出现无监听程序.... 我昨天重新安装Oracle后,用PL/SQL Developer ...
- iOS 10.3下使用Fiddler抓取HTTPS请求
最近使用Fiddler抓取手机上的HTTPS请求时,遇到一个问题.设置完之后,访问HTTPS的页面,却得到一个错误 网络出错,轻触屏幕重新加载:-1202 (如下图所示) 我印象中,之前都好好的, ...