31. 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
链接: http://leetcode.com/problems/next-permutation/
题解:
正常倒序应该是654321,假如652431,则2为inversion,并且next permutation应该为653124。
方法是从后向前找inversion,找到第一个inversion,如上例中,然后继续从后向前判断,假如从数组尾部到inversion元素 i 间有数字大于i, 则swap i 和这个数字,由于swap之后依然是倒序,所以我们reverse i 到 nums.length -1。
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {
public void nextPermutation(int[] nums) { // in place, so we consider swap
if(nums == null || nums.length == 0)
return;
for(int i = nums.length - 2; i >= 0; i--) { //find first inversion from end e.g. - 654231
if(nums[i] < nums[i + 1]) {
for(int j = nums.length - 1; j >= i; j--) {
if(nums[j] > nums[i]) {
swap(nums, i, j); //swap inversion element and swap i, keep descendent order
reverse(nums, i + 1, nums.length - 1); // i + 1 to nums.length - 1 if possible
return;
}
}
}
}
reverse(nums, 0, nums.length - 1); // no inversion
}
private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
private void reverse(int[] nums, int lo, int hi) {
while(lo < hi)
swap(nums, lo++, hi--);
}
}
题外话:
1-20-2016:
最近刷题的劲头减弱,身体也并不很好,由于压力总是大失眠。给Amazon和Microsoft投递出了简历都是石沉大海,好挫败,常常看地里New Graduate很容易就能拿到onsite或者video interview,只回答几个简单问题就能拿到offer,很羡慕。有时候甚至想辞职去读一个名校CS学位。
负能量比较重。
希望二刷时好好锻炼好思维, 沉住气,耐心按照计划执行。已经付出这么多时间和努力,接下来就是要坚持下去。付出 + 坚持,我相信一定会有回报。自己给自己鼓励吧。
二刷:
主要还是用之前的办法。先从数组后部向前部找第一个正序的数对,比如(2, 3),(2, 4)一类。找到这第一个正序队之后,我们要另外设置一个变量j,也是从数组后部向前部查找,找第一个值nums[j] > nums[i],比如(2, 3, 1)。 找到之后我们要swap(i, j),这样就能保持 i + 1到 nums.length - 1这些数字呈现一个降序的排列,然后我们再reverse(i + 1, nums.length - 1)就可以了。假如数组中没有正序的数对,那么我们根据题意要对整个数组进行逆序。
二刷时看了一下discuss区yavinci大神的代码,真是简洁又漂亮,非常羡慕。
Java:
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public void nextPermutation(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
for (int i = nums.length - 2; i >= 0; i--) {
if (nums[i] < nums[i + 1]) {
for (int j = nums.length - 1; j > i; j--) {
if (nums[j] > nums[i]) {
swap(nums, i, j);
reverse(nums, i + 1, nums.length - 1);
return;
}
}
}
}
reverse(nums, 0, nums.length - 1);
}
private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
private void reverse(int[] nums, int i, int j) {
while (i < j) {
swap(nums, i++, j--);
}
}
}
三刷:
Java:
public class Solution {
public void nextPermutation(int[] nums) {
if (nums == null || nums.length < 2) return;
int len = nums.length;
for (int i = len - 2; i >= 0; i--) {
if (nums[i] < nums[i + 1]) {
for (int j = len - 1; j > i; j--) {
if (nums[j] > nums[i]) {
swap(nums, i, j);
reverse(nums, i + 1, len - 1);
return;
}
}
}
}
reverse(nums, 0, len - 1);
}
private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
private void reverse(int[] nums, int i, int j) {
while (i < j) swap(nums, i++, j--);
}
}
Reference:
https://leetcode.com/discuss/8472/share-my-o-n-time-solution
https://leetcode.com/discuss/38247/algorithm-wikipedia-implementation-permutations-permutations
https://leetcode.com/discuss/70881/easiest-java-solution
https://leetcode.com/discuss/47076/1-4-11-lines-c
31. Next Permutation的更多相关文章
- LeetCode - 31. Next Permutation
31. Next Permutation Problem's Link ---------------------------------------------------------------- ...
- [Leetcode][Python]31: Next Permutation
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- <LeetCode OJ> 31. Next Permutation
31. Next Permutation Total Accepted: 54346 Total Submissions: 212155 Difficulty: Medium Implement ne ...
- 刷题31. Next Permutation
一.题目说明 题目是31. Next Permutation,英文太差看不懂,翻译了一下.才知道是求字典顺序下的下一个排列,不允许使用额外空间.题目难度是Medium! 二.我的实现 首先要进一步理解 ...
- leetcode 31. Next Permutation (下一个排列,模拟,二分查找)
题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...
- [LeetCode] 31. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode 【31. Next Permutation】
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- tomcat6.0 数据库连接池配置问题
tomcat6.0 数据库连接池配置问题: 连接池配好后,启动tomat后,输入项目系统的登录名和密码,报 Cannot create JDBC driver of class '' for conn ...
- C#生成随机字符串
//<summary> ///得到随机字符. ///</summary> ///<param name="intLength">Length o ...
- SQLite简易入门
本文内容来源:https://www.dataquest.io/mission/129/introduction-to-sql 本文所用数据来源:https://github.com/fivethir ...
- hg211g破解获取管理员密码,可以连接路由器。默认光猫来拨号。
先通过这种方式获取telecomadmin密码:1.使用useradmin用户登录设备2.在IE地址栏输入该路径”192.168.1.1/backupsettings.html”3.点击保存设备备份配 ...
- iOS:等比压缩截图代码
将一幅图片按着需要的尺寸进行等比的压缩和放大,最后再截取需要尺寸部分,不知道说清楚没,反正就那意思吧! +(UIImage *)compressImageWith:(UIImage *)image w ...
- 内存管理、ARC
内存管理 一.基本原理 1.什么是内存管理 移动设备的内存极其有限,每个app所能占用的内存是有限制的 当app所占用的内存较多时,系统会发出内存警告,这时得回收一些不需要再使用的内存空间.比如回收一 ...
- 自选项目--手机锁屏软件--NABC分析
N(Need 需求) 关键字:利用碎片时间加强对想记的事物的记忆.备忘.一般来说,锁屏目的大致有三点: 1.保护手机隐私 2.防止误操作手机 3.在不关闭系统软件的情况下节省电量 对于市面上已有的锁屏 ...
- 保持程序在后台长时间运行-b
iOS为了让设备尽量省电,减少不必要的开销,保持系统流畅,因而对后台机制采用墓碑式的“假后台”.除了系统官方极少数程序可以真后台,一般开发者开发出来的应用程序后台受到以下限制:1.用户按Home之后, ...
- BOM-字节序标记
BOM——Byte Order Mark 字节序标记 首先是什么是字节序? 字节序:与二进制数据在机器存放位置相关的! 可分为两类: 1. 小端字节序: 低地址放低位数据. x86系列的计算机就使用这 ...
- InterlliJ调试:Method breakpoints may dramatically slow down debugging
问题:Method breakpoints may dramatically slow down debugging 因为此问题久久不能调试.问题发现后原来如此... 原因:设置了方法断点!--什么是 ...