31. Next Permutation 返回下一個pumutation序列

[抄題]:算法

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.數據結構

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).ide

The replacement must be in-place and use only constant extra memory.優化

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.spa

1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1debug

 [暴力解法]:code

時間分析:blog

空間分析:遞歸

 [優化後]:ip

時間分析:

空間分析:

[奇葩輸出條件]:

[奇葩corner case]:

[思惟問題]:

徹底不知道怎麼操做啊:從後往前找遞增,再把遞減的尾巴整個reverse一遍,變成遞增。

[英文數據結構或算法,爲何不用別的數據結構或算法]:

[一句話思路]:

把遞減的尾巴整個reverse一遍,變成遞增。i和後面交換,確保更加遞增

[輸入量]:空: 正常狀況:特大:特小:程序裏處理到的特殊狀況:異常狀況(不合法不合理的輸入):

[畫圖]:

[一刷]:

合理性的角度考慮:reverse(nums, i + 1, len - 1); 則i能夠從-1開始。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

  [五分鐘肉眼debug的結果]:

[總結]:

把遞減的尾巴整個reverse一遍,變成遞增。i和後面交換,確保更加遞增。

[複雜度]:Time complexity: O(n) Space complexity: O(1)

[算法思想:迭代/遞歸/分治/貪心]:

[關鍵模板化代碼]:

[其餘解法]:

[Follow Up]:

[LC給出的題目變變變]:

 [代碼風格] :

 [是否頭一次寫此類driver funcion的代碼] :

 [潛臺詞] :

 

class Solution {
    public void nextPermutation(int[] nums) {
        int len = nums.length;
        
        //corner case
        if (nums == null || len < 2) return ;
        
        //find the first ascending i from len - 2
        int i = len - 2;
        while (i >= 0 && nums[i] >= nums[i + 1]) i--;
        
        if (i >= 0) {
           //find the max j from the len - 1
            int j = len - 1;
            while (nums[i] >= nums[j]) j--;
        
            //swap i & j
            swap(nums, i, j);
        }
            //reverse from i+1 to len - 1
            reverse(nums, i + 1, len - 1); 
            System.out.println("i = " + i);
    }
    
    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 i, int j) {
        while (i < j)
            swap(nums, i++, j--);
    }
}
View Code
相關文章
相關標籤/搜索