实现获取下一个排列函数,这个算法需要将数字重新排列成字典序中数字更大的排列。
如果不存在更大的排列,则重新将数字排列成最小的排列(即升序排列)。
修改必须是原地的,不开辟额外的内存空间。
这是一些例子,输入位于左侧列,其相应输出位于右侧列。
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
详见:https://leetcode.com/problems/next-permutation/description/

Java实现:

class Solution {
public void nextPermutation(int[] nums) {
if(nums==null || nums.length==0){
return;
}
int i = nums.length-2;
while(i>=0 && nums[i]>=nums[i+1]) {
--i;
}
if(i>=0){
int j=i+1;
while(j<nums.length && nums[j]>nums[i]){
++j;
}
--j;
swap(nums,i,j);
}
reverse(nums, i+1,nums.length-1);
}
private void swap(int[] nums,int i,int j){
int tmp=nums[i];
nums[i]=nums[j];
nums[j]=tmp;
}
private void reverse(int[] nums,int i,int j){
while(i<j){
swap(nums,i++,j--);
}
}
}

参考:http://www.cnblogs.com/grandyang/p/4428207.html

031 Next Permutation 下一个排列的更多相关文章

  1. [LeetCode] Next Permutation 下一个排列

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

  2. Next Permutation 下一个排列

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

  3. 【LeetCode每天一题】Next Permutation(下一个排列)

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

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

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

  5. lintcode:next permutation下一个排列

    题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...

  6. [leetcode]31. Next Permutation下一个排列

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

  7. [Swift]LeetCode31. 下一个排列 | Next Permutation

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

  8. 力扣——Next Permutation(下一个排列) python实现

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

  9. Leetcode31--->Next Permutation(数字的下一个排列)

    题目: 给定一个整数,存放在数组中,求出该整数的下一个排列(字典顺序):要求原地置换,且不能分配额外的内存 举例: 1,2,3 → 1,3,2:  3,2,1 → 1,2,3:  1,1,5 → 1, ...

随机推荐

  1. [转]CSS遮罩——如何在CSS中使用遮罩

    特别声明:此篇文章由D姐根据Christian Schaefer的英文文章原名<CSS Masks – How To Use Masking In CSS Now>进行翻译,整个译文带有我 ...

  2. FPGA, Float 32bit, multiplyier by Verilog

    1, FPGA device, using three 18bit x 18 bit multiplier to implement 32bit float multiplier 2, compari ...

  3. (转)python set 用法

    转载自:http://hi.baidu.com/����_xu/blog/item/5b9650c513bd3f049d163d8b .html python的set和其他语言类似, 是一个 基本功能 ...

  4. 用Word编辑cnblogs的博文并发布

    听说可以用Word直接编辑文件发布博客,请教加研究,终于配置成功,先分享如下: 1. 在www.cnblogs.com上注册用户. 2. 打开Word,选择博客文章 3. 点击管理账户,新建或更改,按 ...

  5. 使用Sed抽取MySQL安装文档的目录及行号

    sed -nr  -e '/^2.|^shell/=' -e '/^2.|^shell/p' INSTALL-SOURCE |awk '{if (NR%2==1) x=$1; else printf ...

  6. [快手(AAuto)学习笔记]如何让程序在运行时请求管理员权限(UAC)

    作者:ffsystem 作为(糟糕的)程序猿,习惯写代码解决一些简单事务.正常用批处理就能解决大部分工作,复杂一点用AutoIt 3. 有时候要分发给别人,就需要一个界面.外行你程序写得如何他看不懂, ...

  7. Linux(C/C++)下的文件操作open、fopen与freopen via Boblim

    Linux(C/C++)下的文件操作open.fopen与freopen open是linux下的底层系统调用函数,fopen与freopen c/c++下的标准I/O库函数,带输入/输出缓冲. li ...

  8. 数据库管理员(Database Administrator,简称DBA)基本知识:

    数据库管理员(Database Administrator,简称DBA)基本知识: 一.数据库基础 1. 数据抽象:物理抽象.概念抽象.视图级抽象,内模式.模式.外模式 2. SQL语言包括数据定义. ...

  9. Centos7 忘记密码的情况下,修改root或其他用户密码

    转载:https://blog.csdn.net/wcy00q/article/details/70570043 应用场景 linux管理员忘记root密码,需要进行找回操作. 注意事项:本文基于ce ...

  10. 洛谷-机器翻译-NOIP2010提高组复赛

    题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英文单词,软件会先 ...