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. 通过tarball形式安装HBASE Cluster(CDH5.0.2)——集群安装总览

    1,手动下载压缩包.tar(下载地址),采用tarball形式手工安装集群. 2,共启用13台虚拟机,CentOS6.5 64bit,nn1,nn2,rm1,rm2,dn1,dn2,dn3,dn4,d ...

  2. koa2实现拦截器进行登录前session校验

    //定义允许直接访问的url const allowpage = ['/login','/api/login'] //拦截 function localFilter(ctx) { let url = ...

  3. Windows 2008驱动安装失败的原因及解决方法

    希望这些内容能够帮助各位朋友顺利地在Windows Server 2008系统环境下安装使用好各种设备的驱动程序! 寻找安装失败原因 一般来说,当我们将目标设备的驱动安装光盘正确放置到Windows ...

  4. SpringMVC使用@ResponseBody时返回json的日期格式及可能产生的问题

    http://blog.csdn.net/z69183787/article/details/40375831 遇到的问题: 1 条件: 1.1.表单里有两个时间参数,都是作为隐藏项随表单一起提交: ...

  5. PHP 数组current和next用法

    1.current   当前数组 <?php $transport = array('foot', 'bike', 'car', 'plane'); $mode = current($trans ...

  6. win7 IIS7 发布网站遇到 HTTP 错误 500.19 由于权限不足

    win7 IIS7 发布网站遇到 HTTP 错误 500.19 - Internal Server Error 一看是5xx错误,就知道是服务器的问题,网上查了下,原来是权限问题 解决方法       ...

  7. 使用psutil库监控linux的系统资源和自定义进程的cpu 内存占用。

    #coding=utf8 import time import psutil from pprint import pprint from logger_until import LoggerUnti ...

  8. bash脚本 while语法

    基本语法(比较常见的两种形式): 只要特定条件为真,”while” 语句就会执行 while [ condition ] do command1 command2 command3 done 或者 w ...

  9. spring+hibernate 下载

    http://www.cnblogs.com/haogj/archive/2012/07/28/nhibernate.html-------原文 http://www.springframework. ...

  10. 有人在贴吧问phpmyadmin如何设置插入的时候默认插入1条记录

    在新版phpmyadmin中(我的版本是3.5.1) 插入的时候会提示插入两条,能够方便操作,让你多录入几条数据,如图 然而有人不想要这个界面默认插入两条,如何改为1条或者其他呢? 我审查了这个元素标 ...