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

【题目分析】

题目要求我们对一个数组进行变换,变换后的结果是按照字典序比当前结果大的值中最小的那一个。如果我们不能找到下一个permutation(即当前顺序是这些数字最大的字典序排列),那么直接给出一个逆序的结果。

【思路】

1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

分析这几个例子我们发现,如果一段序列是完全降序排列的,那么它的字典序是最大的,即对于这个序列中任意相邻的两个数a和b,a>=b。如果存在这样相邻的两个数a < b,那么通过变换我们可以找到它的下一个permutation。比如1,2,3 -> 1,3,2 我们交换2和3即可得到下一个结果。但是考虑这样一个序列: 4,6,5,我们看到4比6小,如果我们交换3和6得到的结果是6,4,5,这是我们想要的结果吗?并不是,我们发现,虽然相邻的4和6相邻且6比4大,但是在6的右边,还存在一个5它比4大,但是它比6小,如果我们交换5和4,得到的结果才是正确的。

因此这个过程可以这样来描述:

1. 找到第一个相邻的元组(a,b),满足a < b;

2. 如果这样的元组不存在,则将数组逆序,程序结束;

3. 如果这样的元组存在,我们找到从b开始数组以后的数中比a大的最小的那一个c。

4. 交换a和c,然后把c之后所有的数逆序,得到最后的结果,返回;

例如:[4,6,5,3,2,1]

第一步:找到元组(4,6) 4 < 6

第二步:找到6之后比4大的元素中最小的那个:5

第三步:交换4和5->[5,6,4,3,2,1,1]

第四步:把5之后的数字逆序->[5,1,1,2,3,4,6]

通过上述几个步骤,我们能正确地找出任意一个序列的next greater permutation

【java代码】

 public class Solution {
public void nextPermutation(int[] nums) {
int len = nums.length;
if(len < 2) return;
//找到第一个降序的数
int index;
for(index = len - 2; index >= 0; index--){
if(nums[index] < nums[index+1]) break;
}
//如果没有这样的数则直接返回逆序的结果
if(index < 0){
for(int i = 0,j = len - 1;i < j; i++, j--){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
return;
}
//找到降序的数的右边比它大的数中最小的那一个
int greater = index + 1;
while(greater < len - 1 && nums[greater + 1] > nums[index]){
greater ++;
}
//第一个降序的数和它右边比他大的最小的那个数交换
int temp = nums[index];
nums[index] = nums[greater];
nums[greater] = temp;
//第一个降序数位置之后所有数逆序,得到最后的结果
for(int i = index + 1, j = len - 1; i < j; i++, j--){
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}

LeetCode OJ 31. Next Permutation的更多相关文章

  1. &lt;LeetCode OJ&gt; 31. Next Permutation

    31. Next Permutation Total Accepted: 54346 Total Submissions: 212155 Difficulty: Medium Implement ne ...

  2. leetcode个人题解——#31 Next Permutation

    写这题时脑子比较混乱,重写了一遍wiki大佬的解法. 算法: According to Wikipedia, a man named Narayana Pandita presented the fo ...

  3. [Leetcode][Python]31: Next Permutation

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...

  4. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  5. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  6. LeetCode - 31. Next Permutation

    31. Next Permutation Problem's Link ---------------------------------------------------------------- ...

  7. leetcode 31. Next Permutation (下一个排列,模拟,二分查找)

    题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...

  8. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  9. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  10. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

随机推荐

  1. CoreJavaE10V1P3.6 第3章 Java的基本编程结构-3.6 字符串 String

    String类(java.lang.String)就是Unicode字符序列,例如:"Java\u2122" 3.6.1 Substring 提取子串 String greetin ...

  2. C#编程之“串口通讯多次接收”

    摘要: 主要记录了再C#的串口开发时遇到的问题,以便后续遇到相同问题再重复砍树造轮子. 1.问题场景 板卡和PC间通过UART进行数据通讯,由PC给板卡发送控制命令,板卡返回相应的数据. 2.遇到的问 ...

  3. android移动开发学习笔记(二)神奇的Web API

    本次分两个大方向去讲解Web Api,1.如何实现Web Api?2.如何Android端如何调用Web Api?对于Web Api是什么?有什么优缺点?为什么用WebApi而不用Webservice ...

  4. html5 -----audio标签

    在现在的公司上班需要做一个html5页面,上下可以滑动的,在页面上需要音乐,默认开始音乐播放,点击音乐标签后音乐停止.后来在项目开发中,遇到性能优化的问题,所以我建议大家以后在使用时不要直接在页面中使 ...

  5. openwrt 添加 应用(luci-application)

    openwrt 添加应用的几个步骤如下: (1)在目录 ./feeds/luci/applications 下添加要增加的应用,譬如 "luci-test" (2)里面应该包含以下 ...

  6. Elastarchsearch安装搭建(一)

    Elasticsearch是一个实时分布式搜索和分析引擎.一个基于Apache Lucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好的.功能最全 ...

  7. ElasticSearch(7)-排序

    引用自ElaticSearch权威指南 一.排序 相关性排序 默认情况下,结果集会按照相关性进行排序 -- 相关性越高,排名越靠前. 这一章我们会讲述相关性是什么以及它是如何计算的. 在此之前,我们先 ...

  8. CSS绑定

    css绑定会对元素的CSS类进行操作.在某些情况下这将非常有用,例如:当数值是负的时将其高亮显示. (注:如果如果不想直接更改CSS类,而是只要改其中一个样式,则需要使用style绑定) 示例:使用静 ...

  9. json_encode不编码中文字符的方式

    json_encode($array,JSON_UNESCAPED_UNICODE);

  10. iOS TableView的分割线

    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { [self.tableView setSeparato ...