`
ouqi
  • 浏览: 41347 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

[leetcode]nextPermutation

阅读更多

思路如下:

假设当前给定permutation序列为 a1 a2 a3...an.

  1. 从序列尾部a[n]开始向头部扫描,找到第一个相邻升序序列 a[i-1]< a[i] ; 因为是扫描到的第一个相邻升序,则此时有a[i]>=a[i+1]>=a[i+2]...>=a[n].
  2. 从a[n]逆序遍历至a[i] 找到第一个大于a[i-1]的值a[j] ,交换a[i-1]和a[j].因为a[j]是第一个大于a[i-1]的,所以有a[j+1]至a[n]都小于等于a[i-1]. 即替换后的序列a[1]...a[j],a[i],a[i+1]..a[j-1],a[i-1],a[j+1]...a[n] 有a[i]>=a[i+1]>=a[j-1]>=a[j]>a[i-1]>=a[j+1]...>=a[n].即从i-n的范围仍然是个非升序序列。
  3. 因为我们已经改变a[i-1]的值为其后序列中大于a[i-1]的值中最接近他的。那么剩下的只需将a[i]~a[n]的范围内的值变为升序序列即可。

示例: 3 2 6 5 4 1

我们找到第一个相邻升序序列 2 6 然后找到2的后面第一个大于2的值4 交换。变为 3 4 6 5 2 1 

然后将6 5 2 1变为升序序列,得到最后结果 3 4 1 2 5 6

 

简单说明下正确性吧: 当找到第一个升序序列 2 6,由于是第一个升序序列,所以以6开头的6 5 4 1肯定是非升序的。对于一个非升序的permutation部分,肯定是这部分内最大的了,所以没办法操作他们去获得整个permutation的下一个值,则需要替换前一位2(用后面的大于他的最小值4替换,由于后面部分是递减的,大于他的最小值肯定是从尾往前第一个比他大的)。序列变为 3 4 6 5 2 1,因为4是第一个比他大的,所以1<2.又因为6>5>4>2.所以后面的序列6 5 2 1也是递减的,使用i,j指针 交换为递增的 1 2 5 6 即可。

 

代码

   public void nextPermutation(int[] num) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if(num == null||num.length == 0) return ;
        
        int i = num.length-1;
        for(;i>=1;i--){
            if(num[i]>num[i-1]){
                break;
            }
        }
        int  j = num.length-1;
        int temp;
        if(i>=1){ 
            i = i-1;
            while(num[j]<=num[i]) j--;
            temp = num[i];
            num[i] = num[j];
            num[j] = temp;
            i = i+1;
        }
        j = num.length-1;
        
        while(i<j){
          temp = num[i];
          num[i] = num[j];
          num[j] = temp;
          i++; j--;
        }
        
    }

 

 

分享到:
评论

相关推荐

    31.Next Permutation 下一个排列【LeetCode单题讲解系列】

    31.Next_Permutation_下一个排列【LeetCode单题讲解系列】

    LeetCode最全代码

    # [LeetCode](https://leetcode.com/problemset/algorithms/) !... Up to date (2016-12-18), there are `447` Algorithms / `13` ...31 | [Next Permutation](https://leetcode.com/problems/next-permutation/)| ...

    戳气球leetcode-leetcode:leetcode

    leetcode category other hot keywords:Palindrome(mic), Subsequence Array 螺旋矩阵Spiral Matrix 顺时针打印矩阵 Next Permutation Product of Array Except Self 189.rotate-array 283.move-zero Range Sum ...

    leetcode最大连通域-leetcode:leetcode

    next_permutation.py - 将数字重新排列为按字典顺序排列的下一个更大的数字排列 permutations2.py - 返回所有可能的唯一排列 3sum_2.py - 查找数组中所有唯一的三元组,其总和为零。 3sum.py - 查找数组中所有唯一的...

    leetcode1004-leetcode:leetcode

    Permutation (M) * -&gt; index 주의, 부등호 하나 틀림 33. Search in Rotated Sorted Array (M) * -&gt; 부등호 주의, 부등호 하나 틀림 34. Find First and Last Position of Element in Sorted Array (M) 35. Search ...

    leetcode答案-exercise-book:算法练习记录

    Permutation 解决方法:掌握排列组合的字典序规律,即可。这个规律找不到,我最后还是直接看答案的。 LeetCode: 581. Shortest Unsorted Continuous Subarray 解决方法:无序列中最大最小值 2018-08-17 19:47 ...

    leetcode2sumc-Leetcode_imp_C:Leetcode在C上的实现

    leetcode_0031_next_permutation.c leetcode_0033_search_rotate.c : binary search, offer11 81 leetcode_0034_first_last_pos.c : binary search leetcode_0035_search_inseart.c : binary search leetcode...

    leetcode装最多水-Leetcode:解决了leetcode问题

    Permutation 算法也可用于迭代地查找数组的排列。 也可用于迭代查找数组的排列。 13 . 14 . 15 . 16 . 17 . Algorithm to find kth largest element from an unsorted array in linear time. (1) If the number of ...

    leetcode最大蓄水量-leetcode_note_book:leetcode题目分类及刷题总结

    NextPermutation 下一排序 完成 DFS 题目 说明 状态 NumIslands 岛屿数量 完成 DivideAndConquer 题目 说明 状态 MaxSubArray 最大子序和 完成 Back Tracing 题目 说明 状态 GenerateParenthesis 括号生成 完成 ...

    lrucacheleetcode-LeetCode:这个库用于总结leetcode中遇到的习题,期望按照数据结构和常用方法分成2类,进行总结,

    lru cache leetcode LeetCode 这个库用于总结leetcode中遇到的习题 常用数据结构习题总结 1.线性表 解决进度 ...Next Permutation 公式 13 Permutation Sequence 公式 14 Valid Sudoku 15 Trapping Rain W

    最低加油次数leetcode-Leetcode:力码

    最低加油次数 ...Next_permutation Stack, trick trick TOP 类似斐波拉契,一层层处理 字符串处理 TOP 完全背包 dfs Trick TOP next_permutation dfs or stl next_permutation dfs or stl TOP 排序后的字符串作

    leetcode:leetcode回购

    密码 leetcode回购 树 - - - - 杂凑 - - 二元搜寻 0004_findMedianSortedArrays - SLN 0033_search - SLN 0034_searchRange - SLN 0035_searchInsert - SLN ...0031_nextPermutation - SLN

    LeetCode:LeetCode练习

    STL算法常用函数:分隔组合:next_permutation()是检索当前范围内的划分,并重新排序为下一个替换,leetcode 556下一个元素元素III prev_permutation()是替换指定范围内的序列重新排列它重新排序为上一个序列。...

    lrucacheleetcode-leetcode-journal:记录所有LeetCode挑战的存储库

    lru缓存leetcode 力扣日记 欢迎来到我的 LeetCode 期刊库。 我的每个问题都将包括一个初始计划阶段,在这个阶段我将用我自己的话来...Permutation (Medium) 34. Find First and Last Position of Element in Sorted Arr

    leetcode中国-leetcode:力码

    leetcode中国 LeetCode | LuoGu 题型记录 P1980 计数问题 (数位dp, 朴素算法) P1047 校门外的树 (线段树, ...全排列问题(next_permutation, dfs(最主要需要记住,置位后需要清0(f[i]=0))) P3392 涂国旗(组合) P23

    leetcode分类-contest.js:准备比赛使用!纯JavaScript中的数据结构和算法,零依赖

    nextPermutation 等。 : KMP, RabinKarp : 路径压缩、按秩合并。 : 质数测试、筛选等。 : 阶乘、模阶乘、二项式系数、帕斯卡三角。 : 欧几里得公约数,扩展欧几里得,模拟元。 : create2DArray, create3DArray, ...

    leetcode演示

    NextPermutation SearchInRotatedSortedArray SearchInRotatedSortedArrayII FindMinimumInRotatedSortedArray FindMinimumInRotatedSortedArrayII FindPeakElement SearchRangeInSortedArray Search...

    javalruleetcode-SDE-Problems:标准SDE问题列表

    leetcode SDE-问题 标准 SDE 问题列表 第一天:(数组) 日 问题陈述 解决方案 困难 使用的数据结构 使用的算法 时间复杂度 空间复杂度 补充阅读 在 N 个整数的数组中查找重复项 中等的 大批 不适用 上) O(1) 在不...

    cpp-算法精粹

    Next Permutation Permutation Sequence Valid Sudoku Trapping Rain Water Rotate Image Plus One Climbing Stairs Set Matrix Zeroes Gas Station Candy Majority Element Rotate Array Contains Duplicate ...

Global site tag (gtag.js) - Google Analytics