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. Codeforces Round #433 (Div. 2)【A、B、C、D题】

    题目链接:Codeforces Round #433 (Div. 2) codeforces 854 A. Fraction[水] 题意:已知分子与分母的和,求分子小于分母的 最大的最简分数. #in ...

  2. JQuery的异步回调支持 - Promise、Deferred

    1.Deferred对象: 一般在函数内部进行声明,并在运行过程中改变其状态,例如成功或失败,最终返回Promise对象用于状态监听. 主要方法: Deferred.resolve(param...) ...

  3. [19/04/02-星期二] IO技术_字符流分类总结(含字符转换流InputStreamReader/ OutputStreamWriter,实现字节转字符)

    一.概念 ------->1.BufferedReader/BufferedWriter [参考19.03.31文章] *Reader/Writer-------->2.InputStre ...

  4. spring mvc(4)处理模型数据

    处理模型数据 Spring MVC 提供了以下几种途径输出模型数据: – ModelAndView: 处理方法返回值类型为 ModelAndView时, 方法体即可通过该对象添加 模型数据 – Map ...

  5. Asp.net MVC 自定义错误页面以及return HttpNotFound遇到的问题

    今天在处理mvc 项目404和500页面时,发现我以前比较喜欢用的Return HttpNotFound()没有跳转到我在webconfig中配置的自定义404页面,而且也不会去执行Global中的A ...

  6. sql中table用法

    for c in (select column_value from table(f_split(V_FileID, ','))) loop --若没有填写资格开始结束时间,则填入 select co ...

  7. vsCode中误删了文件,教你怎么恢复

      不要慌!下面开始帮你找到,很简单!

  8. c++基础STL

    今天给大家介绍几个容器,包含的头文件为<vector>,<stack>,<queue>,<map>,<list>,<deque> ...

  9. #leetcode刷题之路14-最长公共前缀

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  10. 复习宝典之SpringMVC

    查看更多宝典,请点击<金三银四,你的专属面试宝典> 第七章:SpringMVC MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(co ...