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. 复习支持向量机(SVM)没空看书时,掌握下面的知识就够了

    支持向量机(support vector machines, SVM)是一种二类分类模型.它的基本模型是定义在特征空间上的间隔最大的线性分类器:支持向量机还包括核技巧,这使它成为实质上的非线性分类器. ...

  2. 欧拉函数-gcd-快速幂(牛客寒假算法基础集训营1-D-小a与黄金街道)

    题目描述: 链接:https://ac.nowcoder.com/acm/contest/317/D来源:牛客网小a和小b来到了一条布满了黄金的街道上.它们想要带几块黄金回去,然而这里的城管担心他们拿 ...

  3. loadrunner整体压测执行操作步骤

    lr11安装包链接:https://pan.baidu.com/s/1hF3j2Vi_xB8BhT70P1ZdBg 提取码:n3zn lr12安装包链接:https://pan.baidu.com/s ...

  4. scrapy的入门使用(一)

    1. scrapy项目实现流程 创建一个scrapy项目:scrapy startproject mySpider 生成一个爬虫:scrapy genspider  提取数据:完善spider,使用x ...

  5. 写给笨蛋徒弟的学习手册(3)—C#中15个预定义数据类型

    在C#中学习中,你会很早的遇到预定义数据类型这个概念,但你有没有仔细想过它存在的意义?正所谓“存在即合理”,预定义数据类型的存在目的主要有俩个方面,一是为了增加程序的安全性,同时减轻编译器负担,加快编 ...

  6. python_flask 基础巩固(自定义URL转换器)

    自定义URL转换器(在BaseConverter类外定义)from werkzeug.routing import BaseConverter定义类继承BaseConverter 实现类app.url ...

  7. api controller 接口接收json字符串参数

    {"data":{"alarmRepeatTimes":2,"currentMode":1,"moduleResetTimeout ...

  8. Freeradius服务器的搭建流程

    Freeradius服务器的搭建流程 一.服务器方面的配置 1 .安装radius服务器,数据库扩展插件 预先安装mysql数据库,然后安装freeradius,以及freeradius的数据库扩展插 ...

  9. 洛谷P1064--金明的预算方案(简单背包)

    https://www.luogu.org/problemnew/show/P1064 #include<iostream> #include<algorithm> #incl ...

  10. CSS+DIV布局中absolute和relative区别

    原文:http://developer.51cto.com/art/201009/225201.htm 这里向大家简单介绍一下CSS+DIV布局中absolute和relative属性的用法和区别,定 ...