import java.util.Arrays;

/**
* Source : https://oj.leetcode.com/problems/next-permutation/
*
* Created by lverpeng on 2017/7/13.
*
* * Implement next permutation, which rearranges numbers into the lexicographically next
* greater permutation of numbers.
*
* If such arrangement is not possible, it must rearrange it as the lowest possible order
* (ie, sorted in ascending order).
*
* The replacement must be in-place, do not allocate extra memory.
*
* Here are some examples. Inputs are in the left-hand column and its corresponding outputs
* are in the right-hand column.
*
* 1,2,3 → 1,3,2
* 3,2,1 → 1,2,3
* 1,1,5 → 1,5,1
*
*/
public class NextPermutation { /**
* 寻找多个数字组成的数字序列中的下一个,比如:1,2,3组成的序列
* 123,132,213,231,312,312
*
* 规律如下:
* 从右向左找到第一个num[i] > num[i-1]
* 然后从右向左找到第一个num[k] > num[i-1]
* 交换两个位置
* 对num[i-1]后面元素排序
*
* 边界条件:i = 1的时候还没找到num[i-1]就把整个数字的各个位翻转顺序
*
*
* @param num
* @return
*/
public void nextPermutation (int[] num) {
for (int i = num.length - 1; i > 0; i--) {
if (num[i] > num[i-1]) {
int k = num.length - 1;
while (num[i-1] > num[k]) {
k --;
}
// swap
int temp = num[i-1];
num[i-1] = num[k];
num[k] = temp;
reverse(num, i, num.length - 1);
return ;
} // 边界情况,已经是最大了,转化为最小
if (i == 1) {
reverse(num, 0, num.length - 1);
return ;
} }
} private void reverse (int[] arr, int start, int end) {
if (start < 0 || end > arr.length - 1 || start > end) {
return;
} while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
end --;
start ++;
}
} public static void main(String[] args) {
NextPermutation nextPermutation = new NextPermutation();
int[] arr1 = new int[]{1, 2, 3, 4};
nextPermutation.nextPermutation(arr1); System.out.println(Arrays.toString(arr1)); int[] arr2 = new int[]{1, 3, 2, 4};
nextPermutation.nextPermutation(arr2);
System.out.println(Arrays.toString(arr2)); int[] arr3 = new int[]{4,3,2,1};
nextPermutation.nextPermutation(arr3);
System.out.println(Arrays.toString(arr3)); }
}

leetcode — next-permutation的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

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

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

  5. LeetCode Palindrome Permutation II

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...

  6. LeetCode Palindrome Permutation

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation/ 题目: Given a string, determine if a per ...

  7. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. [LeetCode] Find Permutation 找全排列

    By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...

  9. [leetcode]Next Permutation @ Python

    原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: Implement next permutation, which rearra ...

  10. LeetCode Find Permutation

    原题链接在这里:https://leetcode.com/problems/find-permutation/description/ 题目: By now, you are given a secr ...

随机推荐

  1. T-3-java核心API-基础类

    一.API 现成的类(程序) Java API是java(Oracle)提供的系统标准API. 第三方的jar包API,如:JUnit.jar. 可以自己开发一些API. 一般情况下任何技术都有现成的 ...

  2. bootstrap的引用和注意事项

    1,在https://v3.bootcss.com/getting-started/#download下载bootstrap的压缩包: 2,将压缩包解压到自己的工程文件中,会得到如下结果: 3,打开这 ...

  3. temp--贵州银行

    -------住宿----泊乐酒店----8905----与朱聿一起住 2018年  1月3日晚 1月4日晚  1月5日晚 1月6日晚  1月7日晚 1月8日晚  1月9日晚 已结清! ======= ...

  4. ffmpeg源码编译安装(Compile ffmpeg with source) Part 2 : 扩展安装

    在Ubuntu,Debian,Mint上编译ffmpeg 本文主要为在Ubuntu,Debian和Mint上编译安装ffmpeg和库文件以及一些扩展的编解码器.当然这与从源中安装无关. 请首先看一下通 ...

  5. Object constraint language for code generation from activity models

    一.基本信息 标题:Object Constraint Language for Code Generation from Activity Models 时间:2018 出版源:Informatio ...

  6. STM32的SWD调试进不了main函数

    玩了那么久STM32,还没有用SWD调试过程序(一直都是用printf调试程序),觉得有些落后了,于是开始搞起了SWD调试. 很快通过查阅资料,知道了keil里面的配置和ST-Link与STM32的连 ...

  7. 了解一下Ubuntu系统

    百度百科: ubuntu系统基于Debian发行版和GNOME桌面环境.Ubuntu的目标在于为一般用户提供一个最新的.同时又相当稳定的主要由自由软件构建而成的操作系统,它可免费使用,并带有社团及专业 ...

  8. Python之旅Day6 模块应用

    time datetime random os sys shutil pickle json shelv xml configparser hashlib subprocess logging re ...

  9. mysql5.6版本的优化

    1. 目标 l 了解什么是优化 l 掌握优化查询的方法 l 掌握优化数据库结构的方法 l 掌握优化MySQL服务器的方法 2. 什么是优化? l 合理安排资源.调整系统参数使MySQL运行更快.更节省 ...

  10. 最容易理解的对卷积(convolution)的解释

    啰嗦开场白 读本科期间,信号与系统里面经常讲到卷积(convolution),自动控制原理里面也会经常有提到卷积.硕士期间又学了线性系统理论与数字信号处理,里面也是各种大把大把卷积的概念.至于最近大火 ...