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. Python从入门到精通之Sixth!

    补充:enumerate 函数用于遍历序列(元组tuple.列表list.字典dict)中的元素以及它们的下标: >>> for i,j in enumerate(('a','b', ...

  2. INTERVAL YEAR TO MONTH数据类型

    INTERVAL YEAR TO MONTH数据类型 Oracle语法: INTERVAL 'integer [- integer]' {YEAR | MONTH} [(precision)][TO ...

  3. 使用mybatis plus 操作数据库

    mybatis plus 是基于mybatis 的一个增强包,比 mybatis 更加容易使用. 特点: 1.分页支持 2.支持自定义查询. 3.简单的情况下,不需要写map.xml 文件 4.支持租 ...

  4. h3c acl配置一列

    acl number 3004 rule 0 permit ip source 10.2.1.4 0 rule 1 deny ip source 192.168.1.91 0 rule 2 deny ...

  5. Git系列:第七篇-Maven项目下提交时忽略不必要的文件或文件夹

    用.gitignore文件来进行忽略不必要的文件或文件夹 在开发中我们要提交的内容大都是src里的全部文件(java文件).gitignore(忽略文件)pom.xml(maven配置文件)----- ...

  6. thinkphp 把小程序码二进制流存储到本地

    public function getxcxm(){ $id = input('id'); $astk = json_decode($this->getasstk())->access_t ...

  7. Unity跳转场景

    Unity中如何加载场景 1.首先需要将场景添加到 Build Settings中,如下图: 2.引用using UnityEngine.SceneManagement; 同步加载:如果场景很大,有可 ...

  8. Chrome 的 PNaCl 还活着么?

    WebAssembly Migration Guide Given the momentum of cross-browser WebAssembly support, we plan to focu ...

  9. Python基础-数据类型-转摘

    1.数字 2 是一个整数的例子.长整数 不过是大一些的整数.3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里,52.3E-4表示52.3 * 10-4.(-5+4j)和(2.3-4. ...

  10. 用JavaScript制作简单的计算器

    <html > <head> <title>简单计算器</title> <style type="text/css"> ...