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. linux fork两次避免僵尸进程的程序(简单)

    #include<stdio.h> #include<unistd.h> int main() { pid_t pid,pid1; int status; ) { printf ...

  2. 13.Linux键盘驱动 (详解)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 在上一节分析输入子系统内的intput_handler软件处理部分后,接下来我们开始写input_dev驱动 本节目标: 实现键盘驱动,让开发板的 ...

  3. IDEA配置Tomcat

    如何用IDEA写Servlet在我别的博文有! 注意:如果不能成功启动Tomcat,很有可能是JDK版本和Tomcat版本不匹配,此时你可以降低JDK版本试试

  4. pygame_polygon

    今天我们要在窗口上绘制简单的多边形 1.认识几个简单的常用的颜色: black=(0,0,0) while=(255,255,255) red=(255,0,0) green=(0,255,0) bl ...

  5. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  6. zoj 1889 ones 数学

    Ones Time Limit: 2 Seconds      Memory Limit: 65536 KB Given any integer 0 <= n <= 10000 not d ...

  7. sqlDependency监控数据库数据变化,自动通知

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. windows下Maven的安装与配置

    1 安装maven前首先确认计算机已正确安装JDK2 下载maven 地址:http://maven.apache.org/download.html安装文件格式如下:apache-maven-3.0 ...

  9. cocos2dx - tmx地图分层移动处理

    接上一节内容:cocos2dx - 节点管理 瓦片地图(Tiled Map) 在cocos2dx文档中有简单的介绍及使用.详情可以看:http://www.cocos2d-x.org/docs/man ...

  10. IOS应用FFMPEG库

    1.引用资源 build-ffmpeg  ffmpeg库生成 -sh开源地址: https://gist.github.com/m1entus/6983547 iFrameExtractor ffmp ...