Problem :寻找给定int数组的下一个全排列(要求:be in-place)
 
倒序查找到该数组中第一个满足后面的数字大于前面的数字的下标i (当前下标 i 指向 后面的那个比较大的数)
 
 
参考代码: 
package leetcode_50;

/***
*
* @author pengfei_zheng
* 下一个全排列
*/
public class Solution31 {
public static void nextPermutation(int[] nums) {
int len = nums.length;
if(len<=1)
return;
int i = len - 1;
for(;i>=1;i--){//从后先前遍历
if(nums[i]>nums[i-1]){
//找到第一个后面的数字大于相邻的前面的那个数的下标 (此时下标指向较大的那个数字)
break;
}
}
if(i!=0){
swap(nums,i-1);//从后向前遍历,交换第一个大于index=i-1的那个数
}
reserver(nums,i);//将从下标i开始的数组进行从小到大的排序
}
private static void swap(int[] nums, int i) {
for(int j=nums.length-1;j>i;j--){//从后向前遍历
if(nums[j]>nums[i]){
int t = nums[j];
nums[j]=nums[i];
nums[i]=t;
break;//交换第一个比前面的index=i-1的那个数
}
}
}
//从下标i开始到nums.length-1结束,实现从小到大排列
private static void reserver(int[] nums, int i) {
int first = i;
int last = nums.length-1;
while(first<last){
int t = nums[first];
nums[first]=nums[last];
nums[last]=t;
first++;
last--;
}
}
public static void main(String[]args){
//int []nums={6,3,4,9,8,7,1};
int []nums={1,2,4,3};
nextPermutation(nums);
for(int n:nums){
System.out.print(n+" ");
}
}
}

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 (下一个字典序排序) 解题思路和方法

    Next Permutation  Implement next permutation, which rearranges numbers into the lexicographically ne ...

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

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

  4. 31. Next Permutation (下一个全排列)

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

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

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

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

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

  7. NextPermutation,寻找下一个全排列

    问题描述:给定一个数组是一个全排列,寻找下一个全排列.例如123->132, 321->123, 115->151. 算法分析:从后往前寻找顺序,找到后从往前寻找第一个大于当前元素, ...

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

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

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

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

随机推荐

  1. php foreach 传值还是传引用

    From: http://my.oschina.net/guomingliang/blog/215457 php 中遍历一个array时可以使用for或foreach,foreach的语法为:fore ...

  2. 3d md5 demo

    描述:场景3dmax做的,随便拖的几个东西 模型玩过游戏的都知道是doom3的怪兽猪脚 音频是openal播放的wav文件 下载地址:http://pan.baidu.com/s/1eQ8SYI2

  3. 最新Java面试题及答案整理

    基础篇 一.基本功 面向对象特征 封装,继承,多态和抽象 1. 封装 封装给对象提供了隐藏内部特性和行为的能力.对象提供一些能被其他对象访问的方法来改变它内部的数据.在 Java 当中,有 3 种修饰 ...

  4. cp -rf 提示覆盖解决办法

    cp覆盖时,无论加什么参数-f之类的还是提示是否覆盖,当文件比较少的时候还可以按Y确认,当很多文件的时候就不好说了 方法一:vi ~/.bashrc # .bashrc # User specific ...

  5. C#文件操作工具类

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

  6. QT 运行崩溃:The inferior stopped because it received a signal from the Operating System

    最近在研究QT自带的boxes例子,自己派生一个图形项,但是在运行生成该图形项时程序直接退出了~ Qt Creater调试代码,问题定位如下代码行: 执行1270行时弹出错误消息框: 于是上网查找资料 ...

  7. 【scala】 scala 映射和元组操作(四)

    1.映射  Map 定义 ,取值,遍历,排序 2. 元组 定义,取值,拉链操作 import scala.collection.mutable /** * 映射和元组 * * @author xwol ...

  8. c++浅拷贝和深拷贝---14

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 1.什么是拷贝构造函数: 拷贝构造函数,又称复制构造函数,是一种特殊的构造函数,它由编译器调用来 ...

  9. [AX2012]代码更改默认财务维度

    在前文(http://www.cnblogs.com/duanshuiliu/p/3243048.html)最后演示了如何使用代码更改默认财务维度,那段代码模拟了创建各数据表记录的过程,实际上AX提供 ...

  10. 【数据分析】Superset 之三 Docker操作管理

    一.进入容器 查看运行的容器:docker ps docker attach confident_thompson 或者 docker attach 34cd2299110f docker exec ...