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

Solution:

http://fisherlei.blogspot.com/2012/12/leetcode-next-permutation.html

算法思想如图所示:

代码如下:

 public class Solution {
public void nextPermutation(int[] num) {
if(num.length<2)
return;
int N=num.length;
int index=N-1;
while(index>0){
if(num[index]<=num[index-1])
index--;
else
break;
}
if(index==0){
Arrays.sort(num);
return;
} int val=num[index-1];
//System.out.println(val);
int j=N-1;
while(j>index-1){
if(num[j]>val){
break;
}
else
j--;
}
// System.out.println(num[j]);
int temp=val;
num[index-1]=num[j];
num[j]=temp;
Arrays.sort(num, index, N);
}
}

[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. DOM – 4.doucument属性

    4.document属性 2.1 write 2.2 getElementById 方法 getElementsByName getElementsByTagName 案例:全选反选 案例:点击一个按 ...

  2. SQL分页存储过程——表名、返回的列、排序字段、排序类型、条件、页尺寸、页码

    ALTER PROCEDURE [dbo].[SP_LGY_ICU_PAGECUT] ), -- 表名 ) = '*', -- 需要返回的列 )='''', -- 排序的欄位名 , -- 設置排序類型 ...

  3. ASP.NET Web API 全局权限和全局异常处理

    在开发中,我使用json格式序列化,所以将默认的xml序列化移除 public static class WebApiConfig { public static void Register(Http ...

  4. Linux系统启动过程分析

    [原创]Linux系统启动过程分析-wjlkoorey258-ChinaUnix博客http://blog.chinaunix.net/uid-23069658-id-3142047.html 经过对 ...

  5. Java Hour 65 [译] Java 6.0 说明

    原文可爱的地址: http://www.javabeat.net/introduction-to-java-6-0-new-features-part-i/ 该文字2007年的,现在估计老掉牙了,但是 ...

  6. php mysql PDO基本操作

    <?php $dbh = new PDO('mysql:host=localhost;dbname=localhost', 'root', ''); $dbh->setAttribute( ...

  7. 让sql语句不排序,按照in语句的顺序返回结果

    SELECT * FROM EVENT WHERE eventId IN(443,419,431,440,420,414,509)  ORDER BY INSTR(',443,419,431,440, ...

  8. 【MongoDB】3.详细命令集合

    [注意:MongoDB自动将_id字段设置为主键] -------------------------------------------------------------------------- ...

  9. 创建一个Portlet工程

    使用Liferay的SDK创建一个简单的Portlet,此Portlet不包括业务逻辑.不包括数据库,只有简单的页面展现,用以说明Portlet的开发过程. 一.创建Portlet工程 1.打开Lif ...

  10. Linux解压文件

    zip: 解压:unzip filename 解压到tmp文件夹:unzip filename.zip -d /tmp 查看压缩文件而不解压:unzip filename.zip -v tar.gz: ...