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. UVa 1331 - Minimax Triangulation(区间DP + 计算几何)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. ASP.NET Web API编程——使用自签名SSL证书

    1自签名SSL证书的创建 创建自签名SSL工具xca为:https://sourceforge.net/projects/xca/ 创建过程 1)创建根证书 打开软件,界面如下. 点击,看到下拉菜单, ...

  3. 使用nuget 打包并上传 nuget.org

    一. 准备工作 1 下载  Download NuGet.exe 2  windows 系统下设置环境变量 path中 或者 在dos 命令窗口下转到 nuget.exe 所在目录 3 在www.nu ...

  4. ASP.NET Core MVC的路由参数中:exists后缀有什么作用,顺便谈谈路由匹配机制

    我们在ASP.NET Core MVC中如果要启用Area功能,那么会看到在Startup类的Configure方法中是这么定义Area的路由的: app.UseMvc(routes => { ...

  5. 一点一点看JDK源码(五)java.util.ArrayList 后篇之removeIf与Predicate

    一点一点看JDK源码(五)java.util.ArrayList 后篇之removeIf与Predicate liuyuhang原创,未经允许禁止转载 本文举例使用的是JDK8的API 目录:一点一点 ...

  6. ObjC之RunTime(上)

    转载自这里. 最近看了一本书——iOS6 programming Pushing the Limits(亚马逊有中文版),最后一章是关于Deep ObjC的,主要内容是ObjC的runtime.虽然之 ...

  7. JavaScript小练习3-用循环使三个DIV变色

    题目 初始为黑色,点击后为红色,再次点击为黑色,以后每次点击一次变色. 分析 简单的onclick使用. button的居中可以在外套一个p元素,body中让p居中即可. 三个DIV块的居中,使用ma ...

  8. Linux中文件函数(二)

    一.link.linkat.unlink.unlinkat.remove函数 创建一个指向现有文件的链接的方法是使用link函数或linkat函数.函数的原型为: #include <unist ...

  9. js扩展String.prototype.format字符串拼接的功能

    1.题外话,有关概念理解:String.prototype 属性表示 String原型对象.所有 String 的实例都继承自 String.prototype. 任何String.prototype ...

  10. 4 二维数组中的查找 JavaScript

    题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...