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. 多线程面试题系列(12):多线程同步内功心法——PV操作上

    上面的文章讲解了在Windows系统下实现多线程同步互斥的方法,为了提高在实际问题中分析和思考多个线程之间同步互斥问题的能力,接下来将讲解PV操作,这也是操作系统中的重点和难点.本文将会先简要介绍下P ...

  2. centos7配置dhcp

    用su 获取root权限 用yum -y install dhcp命令安装dhcp服务(yum是基于RPM包管 理,自动下载RPM包并且安装) 查看安装后生成的配置文件 rpm -qc dhcp 编辑 ...

  3. 接口测试入门(3)--使用httpClient进行登录用例操作/set-cookies验证/ List<NameValuePair>设置post参数/json解析

    (最近学的都是很基础的接口测试,都是基于UI界面可见的接口,就是发请求,接收响应,分析返回的结果,校验,对共通模块进行封装,仅此而已,其实做自动化的思路基本都是如此,UI也是.) 现在开始用httpC ...

  4. Java通过链表实现栈

    class LinkedStack<T> { private Node top; private int size; /** * 初始化栈 */ public LinkedStack() ...

  5. htt p第一章概述

    http的概述 1 web客户端与服务器是如何通信 2 web资源来自的何方 3 web事务是怎样的工作的 4 http通信所使用的报文结构 5 底层tcp的传输的结构 6不同的http协议体 什么是 ...

  6. Rendering Problems Failed to load platform rendering library 为何打开布局页面时手机预览页面显示不出来?

    看到图片右上角的 android图标没有?把它改为低版本的就可以了,如我的是21就可以了.原因我想是因为sdk版本更新了不兼容导致的吧.

  7. Tensorflow之卷积神经网络(CNN)

    前馈神经网络的弊端 前一篇文章介绍过MNIST,是采用的前馈神经网络的结构,这种结构有一个很大的弊端,就是提供的样本必须面面俱到,否则就容易出现预测失败.如下图: 同样是在一个图片中找圆形,如果左边为 ...

  8. 微信bug:建议了解,不要实验,不要手贱,不要。。。。

    今天下午在群里聊天的时候,群友反应发现微信的一个bug:使用微信给好友发送‘15...............’(数字15后面加15个句号)会导致微信运行缓慢,到最后的应用未响应,退出微信. 解决办法 ...

  9. [js高手之路] html5新增的定时器requestAnimationFrame实战进度条

    在requestAnimationFrame出现之前,我们一般都用setTimeout和setInterval,那么html5为什么新增一个requestAnimationFrame,他的出现是为了解 ...

  10. Iframe刷新页面

    window.parent.frames["name"].location="url";