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. Win10无法使用内置管理员用户打开edge解决方案

    https://jingyan.baidu.com/article/4f7d5712d23f1b1a2119274b.html

  2. oracle 杀掉当前用户的进程

    select ' alter system kill session '''|| sid ||','||serial# || ''';' from v$SESSION where username=' ...

  3. python的sciter库Pysciter安装教程(win32 + win64)

    Pysciter是一个结合HTML与Python编写桌面软件个三方库 注:无论是32位还是64位电脑,建议安装32位的sciter,这样写出来的软件可以在win32和win64电脑上都可以运行(前提p ...

  4. node配置自动监测文件改变不重启

    方法一: nodemon npm install -g nodemon nodemon ./bin/www 或者在npm start命令里把node改为nodemon 方法二:supervisor n ...

  5. Synycovery 7.18f 一个优秀的同步软件

    Serial Key Name: Vdown RG Code: MCKOFA7MNGUQY7954

  6. centos6.5环境 安装php5.5.30的redis扩展 介绍

    1.下载软件包 wget http://pecl.php.net/get/redis-2.2.5.tgz       2.解压 tar zxvf redis-2.2.5.tgz        3.进入 ...

  7. shell脚本自动清理服务器日志、图片等信息

    在做性能测试的时候,linux服务器时常会产生大量数据,如日志信息,图片信息,文件信息等,压测一段时间后,导致服务器磁盘空间暂满而崩溃,每天手动清理比较麻烦, 利用shell脚本自动清理,脚本如下 1 ...

  8. 完美解决ListView中事件ItemCreated中使用ClientID导致插入数据失败

    于昨天晚上看到视频做到这个例子,但是发现始终有错误,在ListView的ItemCreated事件中使用了ClientID则会导致数据插入数据库失败.当点击插入按钮时,网页就像点击F5刷新一样,无任何 ...

  9. Innodb表压缩过程中遇到的坑(innodb_file_format)

    https://www.cnblogs.com/billyxp/p/3342969.html

  10. LeetCode_Maximum Subarray | Maximum Product Subarray

    Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...