题目:

实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。

如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。

必须原地修改,只允许使用额外常数空间。

以下是一些例子,输入位于左侧列,其相应输出位于右侧列。
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

【0】【1】【2】

解题思路:

题目的总体思路是。从后往前读。当后面的数比前面的数大时,再开一个循环,从后开始于当前数比較。当比当前数大时,交换。然后再从当前数的后一位開始,直到最后反序就可以。

  1. class Solution {
  2. public void nextPermutation(int[] nums) { //高位为nums[0]
  3. if(nums != null && nums.length >1){
  4. int i;
  5. for(i = nums.length-2;i>=0;i--){
  6. if(nums[i+1]>nums[i]){
  7. break;
  8. }
  9. }
  10.  
  11. if(i >= 0){ //如果整个序列为逆序时,i小于0 reverse整个序列,否则找到比nums[i]大的交换次序
  12. int k;
  13. for(k=nums.length-1;k>=0;k--){
  14. if(nums[k]>nums[i]){
  15. break;
  16. }
  17. }
  18. swap(nums,k,i);
  19. }
  20. reverse(nums, i+1);
  21. }
  22. }
  23.  
  24. private void reverse(int[] nums, int start) {
  25. int left = start;
  26. int right = nums.length - 1;
  27. while (left < right) {
  28. swap(nums, left, right);
  29. left++;
  30. right--;
  31. }
  32. }
  33.  
  34. private void swap(int[] nums, int i, int j) {
  35. int temp = nums[i];
  36. nums[i] = nums[j];
  37. nums[j] = temp;
  38. }
  39. }

leetcode 31. Next Permutation JAVA的更多相关文章

  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. Java [leetcode 31]Next Permutation

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

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

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

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

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

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

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

  9. leetcode 31. Next Permutation(字典序的下一个)

    描述: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

随机推荐

  1. iOS学习之UIPickerView控件的关联选择

    接上篇iOS学习之UIPickerView控件的简单使用 接着上篇的代码 http://download.csdn.net/detail/totogo2010/4391870 ,我们要实现的效果如下: ...

  2. rocketmq消息存储概述

    了解消息存储部分首先需要关注的几个方法,load()--Load previously stored messages.start()--Launch this message store.putMe ...

  3. spring4-2-bean配置-4-bean之间的关系

  4. 关于select Count()的使用和性能问题

    比如Count(*) FROM E_Table WHERE [date] > '2008-1-1' AND istrue = 0 由于操作的数据比较大(400万以上),所以使用了两个数据库,一个 ...

  5. CF 662C Binary Table

    用FWT优化计算. 首先发现行数很小,想到一个暴力的方法,就是以一个二进制位$0$表示这一行不翻转而二进制位$1$表示这一行翻转,然后$2^n$枚举出所有行的翻转情况,再$O(m)$计算所有的结果. ...

  6. 最小生成树 prim

    1.算法思想: 图采用邻接矩阵存储,贪心找到目前情况下能连上的权值最小的边的另一端点,加入之,直到所有的顶点加入完毕. 2.算法实现步骤: 设图G =(V,E),其生成树的顶点集合为U. (1)把v0 ...

  7. NPOI读写Excel sheet操作

    QueryInfo dataInfo = new QueryInfo(); dataInfo.CustomSQL = $@" select t1.name name,t1.url url f ...

  8. easyui tabs update 强制刷新页面

    var tab = artDialog.open.origin.$("#tabs").tabs("getTab", "公司评级"); var ...

  9. CentOS7.4配置SSH登录密码与密钥身份验证踩坑

    简单记录,自用CentOS7.4虚拟机与ALiYunVPS,在配置ssh登录身份验证时碰到的问题. 阿里云VPS:因为在重置磁盘时选择了密钥对的身份验证方式,因此VPS中的CentOS7.4中的 /e ...

  10. springdata -----操作ES

    一:配置springdata-Es elasticseach-JPA.xml <?xml version="1.0" encoding="UTF-8"?& ...