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. commons dbcp.jar有什么用

    主流数据库连接池之一(DBCP.c3p0.proxool),单独使用DBCP需要使用commons-dbpc.jar.commons-collections.jar.commons-pool.jar三 ...

  2. anydesk重启脚本

    1.restart.sh: #!/bin/sh sh /home/zhoushuo/anydeskTest/stop.sh echo 'zhoushuo'|sudo -S /usr/bin/anyde ...

  3. Software Architecture

    Software Architecture Architecture serves as a blueprint for a system. It provides an abstraction to ...

  4. What is Systems Architecture ?

    What is Systems Architecture ?   Systems Architecture is a generic discipline to handle objects (exi ...

  5. select、poll和epoll比较

    select select能监控的描述符个数由内核中的FD_SETSIZE限制,仅为1024,这也是select最大的缺点,因为现在的服务器并发量远远不止1024.即使能重新编译内核改变FD_SETS ...

  6. php------中文出现乱码解决方法

    中文出现乱码解决方法:原因编码格式不一致 [1]  建立数据库的时候,字符集选择utf-8  数据库,点表名,点右键,数据库属性…. [2]  修改myspl的配置,在[myspld]模块下面添加ch ...

  7. 禁止查看网页源代码和F12

    function disableInfo() { document.onkeydown = function() { var e = window.event || arguments[0]; //屏 ...

  8. PHP验证信用卡卡号函数

    /** * 验证银行卡号是否是信用卡 * @param $cardnumber * @return bool */ function validateCard ($cardnumber) { $car ...

  9. Windows 2008 Scheduled tasks result codes

    0 or 0x0: The operation completed successfully. 1 or 0x1: Incorrect function called or unknown funct ...

  10. Git--将服务器代码更新到本地

    1. git status(查看本地分支文件信息,确保更新时不产生冲突) 2. git checkout -- [file name] (若文件有修改,可以还原到最初状态; 若文件需要更新到服务器上, ...