LeetCode 31 Next Permutation(下一个全排列)
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(下一个全排列)的更多相关文章
- [LeetCode] 31. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically ne ...
- [leetcode]31. Next Permutation下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 31. Next Permutation (下一个全排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- NextPermutation,寻找下一个全排列
问题描述:给定一个数组是一个全排列,寻找下一个全排列.例如123->132, 321->123, 115->151. 算法分析:从后往前寻找顺序,找到后从往前寻找第一个大于当前元素, ...
- leetcode 31. Next Permutation(字典序的下一个)
描述: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- LeetCode 31. Next Permutation (下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- php foreach 传值还是传引用
From: http://my.oschina.net/guomingliang/blog/215457 php 中遍历一个array时可以使用for或foreach,foreach的语法为:fore ...
- 3d md5 demo
描述:场景3dmax做的,随便拖的几个东西 模型玩过游戏的都知道是doom3的怪兽猪脚 音频是openal播放的wav文件 下载地址:http://pan.baidu.com/s/1eQ8SYI2
- 最新Java面试题及答案整理
基础篇 一.基本功 面向对象特征 封装,继承,多态和抽象 1. 封装 封装给对象提供了隐藏内部特性和行为的能力.对象提供一些能被其他对象访问的方法来改变它内部的数据.在 Java 当中,有 3 种修饰 ...
- cp -rf 提示覆盖解决办法
cp覆盖时,无论加什么参数-f之类的还是提示是否覆盖,当文件比较少的时候还可以按Y确认,当很多文件的时候就不好说了 方法一:vi ~/.bashrc # .bashrc # User specific ...
- C#文件操作工具类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- QT 运行崩溃:The inferior stopped because it received a signal from the Operating System
最近在研究QT自带的boxes例子,自己派生一个图形项,但是在运行生成该图形项时程序直接退出了~ Qt Creater调试代码,问题定位如下代码行: 执行1270行时弹出错误消息框: 于是上网查找资料 ...
- 【scala】 scala 映射和元组操作(四)
1.映射 Map 定义 ,取值,遍历,排序 2. 元组 定义,取值,拉链操作 import scala.collection.mutable /** * 映射和元组 * * @author xwol ...
- c++浅拷贝和深拷贝---14
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 1.什么是拷贝构造函数: 拷贝构造函数,又称复制构造函数,是一种特殊的构造函数,它由编译器调用来 ...
- [AX2012]代码更改默认财务维度
在前文(http://www.cnblogs.com/duanshuiliu/p/3243048.html)最后演示了如何使用代码更改默认财务维度,那段代码模拟了创建各数据表记录的过程,实际上AX提供 ...
- 【数据分析】Superset 之三 Docker操作管理
一.进入容器 查看运行的容器:docker ps docker attach confident_thompson 或者 docker attach 34cd2299110f docker exec ...