题目:

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

题意:

产生下一个序列,对给定序列进行重排,生成一个字母顺序比它更大的下一个序列。

如果给定的序列已经是按字母顺序排列中最大的一个了,则进行逆序排列。

算法在数组内进行,不要使用额外空间。

算法设计:

(1)从后向前遍历,找到第一个不满足降序的元素;若初始序列全部是降序,则i为-1,直接跳转至(3);

(2)将该元素同它后面的元素中比它大的第一个元素交换;

(3)将该元素后的所有元素排列,使之成为最小的排列。

代码:

    public void nextPermutation(int[] nums) {
if(nums == null || nums.length == 0)
return;
//长度为1的数组
if (nums.length == 1) {
return;
} //从后向前找到第一个不满足逆序的元素
int i = nums.length - 2;
for(; i >= 0 && nums[i] >= nums[i + 1]; --i); //注意,这里有=,可以排除含有重复元素的情况 //从i+1位置开始,向后查找比nums[i]大的最小元素
if(i >= 0){
int j = i + 1;
for(; j < nums.length && nums[j] > nums[i]; ++j);
swap(nums,i,j - 1); //交换,注意是同 j - 1交换
} //将i之后的元素逆置(这里包含一种特殊情况,若该排列已经是字典序中的最大了,则下一个序列应该是最小字典序,因此,直接在这里逆置即可)
int k = nums.length - 1;
i++;
for(; i < k; i++, k--)
swap(nums, i, k);
} /**
* 交换元素
* @param array
* @param i
* @param j
*/
public void swap(int[] array,int i ,int j){
//交换
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}

31. Next Permutation (java 字典序生成下一个排列)的更多相关文章

  1. POJ 1146 ID Codes 用字典序思想生成下一个排列组合

    ID Codes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7644   Accepted: 4509 Descript ...

  2. POJ:1833 按字典序找到下一个排列:

    http://poj.org/problem?id=1833 按照字典的顺序(a-z) (1-9),可以得出任意两个数字串的大小.比如“123”, 最小的是“123”(从小到大),最大的是“321”( ...

  3. next_permutation():按字典序输出下一个排列

    #include<iostream> #include<algorithm> using namespace std; int main() { int data[4]={5, ...

  4. leetcode 31. Next Permutation(字典序的下一个)

    描述: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

  5. LeetCode:下一个排列【31】

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

  6. SGU 179 Brackets light(生成字典序的下一个序列)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=179 解题报告:输入一个合法的括号串,求出这个括号串的字典序的下一个串.(认为'(' ...

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

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

  8. Java实现 LeetCode 31下一个排列

    31. 下一个排列 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许 ...

  9. LeetCode 31. 下一个排列(Next Permutation)

    题目描述 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常 ...

随机推荐

  1. HDU 5929 Basic Data Structure(模拟 + 乱搞)题解

    题意:给定一种二进制操作nand,为 0 nand 0 = 10 nand 1 = 1 1 nand 0 = 1 1 nand 1 = 0 现在要你模拟一个队列,实现PUSH x 往队头塞入x,POP ...

  2. Css下拉菜单设置

    <style type="text/css"> *{margin:0px;padding:0px;}设置所有标签属性<初始化页面属性> #nav{backg ...

  3. 物体检测算法 SSD 的训练和测试

    物体检测算法 SSD 的训练和测试 GitHub:https://github.com/stoneyang/caffe_ssd Paper: https://arxiv.org/abs/1512.02 ...

  4. Js 运行机制 (重点!!)

    一.引子 本文介绍JavaScript运行机制,这一部分比较抽象,我们先从一道面试题入手: 这一题看似很简单,但如果你不了解JavaScript运行机制,很容易就答错了.题目的答案是依次输出1 2 3 ...

  5. git代码回滚的两种选择

    回滚到指定commit,且保持该commit之前和之后的提交记录 使用git revert命令. git revert HEAD // 回滚到前一次 commit git revert HEAD^ / ...

  6. Spring-Mybatis依赖

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --><dependency> <groupId& ...

  7. Error: Program type already present: okhttp3.Authenticator$1

    在app中的build.gradle中加入如下代码, configurations { all*.exclude group: 'com.google.code.gson' all*.exclude ...

  8. eclipse中.project文件和.classpath文件详解

    一.概述.project是项目文件,项目的结构都在其中定义,比如lib的位置,src的位置,classes的位置..classpath的位置定义了你这个项目在编译时所使用的$CLASSPATH. 二. ...

  9. hibernate的三种状态和缓存

    hibernate的三种状态: 1.瞬时态:对象里面没有id值,对象与session没有关联 类似,把class类new出来,不存进session 2.持久态:对象里面有id值,对象与session关 ...

  10. HDU 2569(简单的递推)

    彼岸 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...