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

 


题目标签:Array

  这道题目给了我们一个array, 让我们in place 更改,改成 下一个更大的排列。 题目中给的例子都太短了。我们来挑一个长一点的例子。
  
  ,7,,3,2  如何找到它的下一个更大的排列呢, 我们看4后面的, 7,5,3,2已经是最大的可能性了,所以只能把4换了,那只能找比4大一点的数字,5。那么结果就是
  5,7,4,3,2  这是把4和5交换之后的结果,但是这个不是4,7,5,3,2的下一个大的排列,我们需要得到的是5开头的最小的排列,所以要把5之后的array sort 一下,就变成
  5,2,3,4,7  这个就是答案。
 
  我们可以看出,第一步是从右边开始找,找到第一个数字,它不在从小到大的顺序里(这里从右向左看), 例子中就是 4。 如果没有找到这个数字的话,那么意味着 这个排列它是 (从左往右) 从大到小的排列。就像原题中给的例子3,2,1。 这样的话,只需要sort一下,让它恢复到最小的第一种可能性。
  接着,在找到了第一个数字4 的话,再一次从右边开始找,找到第一个比4大的数字,就是5,把这两个数字交换一下。
  最后,把5之后的array 给sort 一下,为什么要sort呢 -> 虽然我们把5换上来了,但是我们需要的仅仅是 下一个更大的排列,那么就需要把5开头的排序保持最小的值。
 
 

Java Solution:

Runtime beats 85.79%

完成日期:07/13/2017

关键词:Array

关键点:找到需要被增大的那个数字A,再找到最接近于A且比A大的数字B,互换A和B,最后把B之后的所有数字sort

 public class Solution
{
public void nextPermutation(int[] nums)
{
// from right to left, find the first number which is not in ascending order
for(int i=nums.length-1; i>=0; i--)
{
if(i == 0) // meaning all number are in ascending order
{
Arrays.sort(nums);
return;
}
else if(nums[i] > nums[i-1])
{
// from right to left, find the first number is greater than nums[index_num]
for(int j=nums.length-1; j>=i-1; j--)
{
if(nums[j] > nums[i-1]) // swap them
{
int temp = nums[i-1];
nums[i-1] = nums[j];
nums[j] = temp;
// sort the rest numbers after i - 1
Arrays.sort(nums, i, nums.length);;
return;
}
}
}
}
} }

参考资料:

http://www.cnblogs.com/grandyang/p/4428207.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

  
  

LeetCode 31. Next Permutation (下一个排列)的更多相关文章

  1. [LeetCode] 31. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  2. [leetcode]31. Next Permutation下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  3. leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

    Next Permutation  Implement next permutation, which rearranges numbers into the lexicographically ne ...

  4. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  5. 【LeetCode每天一题】Next Permutation(下一个排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. LeetCode(31): 下一个排列

    Medium! 题目描述: (请仔细读题) 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列) ...

  7. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  8. 031 Next Permutation 下一个排列

    实现获取下一个排列函数,这个算法需要将数字重新排列成字典序中数字更大的排列.如果不存在更大的排列,则重新将数字排列成最小的排列(即升序排列).修改必须是原地的,不开辟额外的内存空间.这是一些例子,输入 ...

  9. lintcode:next permutation下一个排列

    题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...

  10. 31. Next Permutation (下一个全排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

随机推荐

  1. Fabric 1.0的多机部署

    Fabric1.0已经正式发布一段时间了,官方给出的单机部署的脚本也很完备,基本上傻瓜式的一键部署,直接运行官方的network_setup.sh up即可.但是在实际生产环境,我们不可能把所有的节点 ...

  2. 安装wampserve之前需要安装vc++2012.

    本人是64位系统下载了wampserver3.0.6之后安装好,启动报错缺少msvcr110.dll. 于是从网上下载了msvcr110.dll放到了windows的syswow64文件夹下,甚至还重 ...

  3. 私人订制——属于你自己的Linux

    一.前言 Linux操作系统至1991年10月5日诞生以来,就其开源性和自由性得到了很多技术大牛的青睐,每个Linux爱好者都为其贡献了自己的一份力,不管是在Linux内核还是开源软件等方面,都为我们 ...

  4. Python-老男孩-02_装饰器_面向对象_封装_继承_异常_接口_数据库

    装饰器其实也是一个函数,它的参数是一个函数 ; 其它函数与装饰器之间建立联系是通过 @装饰器函数名, 感觉有点像Spring的面向切面编程 装饰器函数,如何处理原函数的参数.?  装饰器 原函数返回值 ...

  5. Activiti-01

    1, Activiti官网:http://www.activiti.org/  主页可以看到jar包的下载. 2, 进入http://www.activiti.org/userguide/index. ...

  6. python 实现注册程序

    本文介绍用python实现一个模拟注册的程序,详细需求如下: # 写一个注册的程序,输入username,密码,密码确认,输入的账号和密码不能为空,两次输入密码必须一致,用户名不能重复,错误次数4次# ...

  7. D - DZY Loves Hash CodeForces - 447A

    DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the ...

  8. Android Studio 导入应用时报错 Error:java.lang.RuntimeException: Some file crunching failed, see logs for details

    在app文件夹的build.gradle里加上 android { ...... aaptOptions.cruncherEnabled = false aaptOptions.useNewCrunc ...

  9. 表空间tablespace,数据文件datafiles,和控制文件control files介绍

    https://docs.oracle.com/cd/B19306_01/server.102/b14220/physical.htm#i5919 本文系翻译 表空间tablespace,数据文件da ...

  10. BZOJ-5055-膜法师(离散化+树状数组)

    Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然,他能为长者所续的时间,为这三个维度上能量的乘 ...