1. 原题链接

https://leetcode.com/problems/next-permutation/description/

2. 题目要求

给出一个整型数组,让我们给出下一个排序情况。注意以下规则:

(1)下一个排列必须比原排列要大。例如“1,2,4,5,3”,下一个排列为“1,3,4,5,2”,比之前的排列要大;

(2)如果给出的数组已经按降序排列,则下一个排列必须是升序排列。例如“5,4,3,2,1”为降序,下一个排列必须为升序“1,2,3,4,5”;

全排列概念:

3. 解题思路

从后向前找到,前一个元素 nums[ i ] 小于后一个元素 nums[ i+1 ] 的位置。然后在从后向前找到 刚好大于nums[ i ] 的元素 nums [ j ],交换 nums[ i ] 和 nums[ j ] 。最后将nums[ i ] 之后的元素进行逆置。

4. 代码实现

 public class NextPermutation31 {
public static void main(String[] args) {
int[] nums ={1,2,4};
NextPermutation31 np =new NextPermutation31();
for(int x:np.nextPermutation(nums))
System.out.println(x); }
public int[] nextPermutation(int[] nums) { int i = nums.length - 2;
while (i >= 0 && nums[i + 1] <= nums[i]) {
i--;
} if (i >= 0) {
int j = nums.length - 1;
while (j >= 0 && nums[j] <= nums[i]) {
j--;
}
swap(nums, i, j);
}
reverse(nums,i+1,nums.length-1); return nums; } public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
} public void reverse(int[] nums, int start, int end) {
while (start < end) {
swap(nums, start, end);
start++;
end--;
}
} }

LeetCode: 31. Next Permutation (Medium)的更多相关文章

  1. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  2. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  3. LeetCode - 31. Next Permutation

    31. Next Permutation Problem's Link ---------------------------------------------------------------- ...

  4. leetcode 31. Next Permutation (下一个排列,模拟,二分查找)

    题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...

  5. LeetCode 31. Next Permutation【Medium】

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

  6. [LeetCode] 31. Next Permutation 下一个排列

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

  7. leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

    Next Permutation  Implement next permutation, which rearranges numbers into the lexicographically ne ...

  8. LeetCode 31. Next Permutation (下一个排列)

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

  9. Java [leetcode 31]Next Permutation

    题目描述: Implement next permutation, which rearranges numbers into the lexicographically next greater p ...

随机推荐

  1. hdu 6208 The Dominator of Strings【AC自动机】

    hdu 6208 The Dominator of Strings[AC自动机] 求一个串包含其他所有串,找出最长串去匹配即可,但是匹配时要对走过的结点标记,不然T死QAQ,,扎心了.. #inclu ...

  2. hdu-3524 Perfect Squares---打表+找规律+循环节

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3524 题目大意: 求i^2 mod 2^n有多少可能 解题思路: 先打表,求出n较小的时候的数据 n ...

  3. 区间DP学习总结

    这段时间学习了区间DP,所以试着把学到的东西稍作总结,以备不时之需. 学习区间DP首先要弄清区间DP是为了解决什么问题:一般的DP主要是特征是一次往往只操作一个数值或者存储可以不连续的物品的状态(比如 ...

  4. rc.local 开机自启脚本无法启动

    1.看是否rc.local 第一行如果是 #!/bin/sh -e 改成 #!/bin/bash sudo systemctl enable rc-local.service 2.看rc.local的 ...

  5. selenium基础知识(概述、安装、IDE等)

    参考http://www.yiibai.com/selenium/selenium_webdriver.html

  6. springboot项目pom添加依赖

    在dependency 后面  ALt+/  可以打开编辑窗口.

  7. VS 2017 + EF6 + MySQL5.7 建立实体模型闪退问题

    具体环境是:VS2017 ..NET Framework 4.6 .MySql.Data.Entity 6.9.12 在这个环境下总是不成功,具体是在这一步闪退,也不报错: 在点击“下一步”后,没有进 ...

  8. 控制DIV中的文字绝对居中

        水平居中:<div align="center">Content</div>       垂直居中:<div style="line ...

  9. C#的常用类

    BitConverter类:用于将源类型转换成字节数组,或者将字节数组转换成目标类型.在解决不同设备之间产生的大小端问题时,经常使用. Convert类:用于基本数据类型(包括Boolean/Byte ...

  10. plsql中特殊字符的处理

    --去除换行chr(10) update zhzl_address t set t.add_administration_num=replace(t.add_administration_num,ch ...