题目:

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. SpringBoot 利用过滤器Filter修改请求url地址

    要求: 代码中配置的url路径为http://127.0.0.1/api/associates/queryAssociatesInfo 现在要求http://127.0.0.1/associates/ ...

  2. The Mathematics of the Rubik’s Cube

    https://web.mit.edu/sp.268/www/rubik.pdf Introduction to Group Theory and Permutation Puzzles March ...

  3. 【调优】kafka性能调优

    主要优化原理和思路 kafka是一个高吞吐量分布式消息系统,并且提供了持久化.其高性能的有两个重要特点: 利用了磁盘连续读写性能远远高于随机读写的特点: 并发,将一个topic拆分多个partitio ...

  4. 关于C#引用ExceptionPolicy.HandleException(ex, "LogAndReplace", out exceptionToReplace);

    http://www.cnblogs.com/Terrylee/archive/2006/07/03/enterprise_library2_1.html 要使用ExceptionPolicy.Han ...

  5. (转) Learning Deep Learning with Keras

    Learning Deep Learning with Keras Piotr Migdał - blog Projects Articles Publications Resume About Ph ...

  6. Facebook ads_Business Manager

    xmind 下载链接

  7. Linux中.rar文件解压

    1. 下载: https://www.rarlab.com/download.htm 我下载的是RAR 5.61 for Linux x64 2. 安装: 解压:tar -zxvf rarlinux- ...

  8. JS基础---到底什么是闭包?它是如何形成的?

    1.闭包 先看一个简单的例子 function a() { var i = 0; function b() { alert(++i); } return b; }var c = a(); c(); 这 ...

  9. @Value("#{}")与@Value("${}")的区别以及用法

    package com.ieou.capsule_basic_info.util; import org.springframework.beans.factory.annotation.Value; ...

  10. Spring boot2.0 设置文件上传大小限制

    今天把Spring boot版本升级到了2.0后,发现原来的文件上传大小限制设置不起作用了,原来的application.properties设置如下: spring.http.multipart.m ...