1. 程式人生 > >全排列,字典順序問題 ( permutations/leetcode)

全排列,字典順序問題 ( permutations/leetcode)

46. Permutations

注意是distinct numbers,所以相對簡單, 當然寫出好的程式碼也是困難的,下面是ac程式碼(效率可能不高)

方法1:採用dfs遍歷

class Solution {
private:
    vector<vector<int>> ans;
    vector<int> rs;
    map<int,int> vis;

public:

    void dfs(int len, vector<int> nums, int n)
    {
        if (len == n){
            ans.push_back(rs);
            return
; } for (int i = 0; i < n; i++) { if (vis[i] == 0){ vis[i] = 1; rs.push_back(nums[i]); dfs(len + 1, nums, n); // dfs rs.pop_back(); vis[i] = 0; } } } vector
<vector<int>>
permute(vector<int>& nums) { int n = nums.size(); if (n == 0) return ans; dfs(0, nums, n); return ans; } };

方法2: 全排列思路,就是以某個數開頭,剩下的全排列,遞迴來做

class Solution {
private:
    vector<vector<int>> ans;

public
: void recursion(int k,vector<int> nums, int n) { if (k >= n) return; if (k == n - 1){ /* for (int i = 0; i < n; i++) cout << nums[i] << " "; cout << endl;*/ ans.push_back(nums); } for (int i = k; i < n; i++) { swap(nums[k], nums[i]); recursion(k + 1, nums, n); swap(nums[k], nums[i]); } } vector<vector<int>> permute(vector<int>& nums) { // nums是distinct int n = nums.size(); if (n == 0) return ans; recursion(0, nums, n); return ans; } };

47. Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2] have the following unique permutations:

    [
      [1,1,2],
      [1,2,1],
      [2,1,1]
    ]

有重複數字,採用遞迴做法,顯然有很多重複的swap操作,需要避免

ac程式碼:

class Solution {
private:
    vector<vector<int>> ans;
public:

    void recursion(int k, vector<int> nums, int n)
    {
        if (k >= n)
            return;
        if (k == n - 1){
            ans.push_back(nums);
        }

        sort(nums.begin() + k, nums.end()); // k之後的元素需要排序
        // 兩相同元素 , 一個元素與兩個相同的元素?
        for (int i = k; i < n; i++)
        {
            if (i != k && nums[i] == nums[i-1])
                continue;
            swap(nums[k], nums[i]);
            recursion(k + 1, nums, n);  // 遞迴
            swap(nums[k], nums[i]);
        }
    }

    vector<vector<int>> permuteUnique(vector<int>& nums) {
        int n = nums.size();
        if (n == 0)
            return ans;

        recursion(0, nums, n);
        return ans;
    }
};

31. Next Permutation

求下一個字典序

ac程式碼:

class Solution {
public:

    void rev(vector<int> &nums, int left, int right){
        for (int i = left; i <= (left + right) / 2; i++){
            int tmp = nums[i];
            nums[i] = nums[right - i + left];
            nums[right - i + left] = tmp;
        }
    }

    void nextPermutation(vector<int>& nums) {
        int n = nums.size();
        if (n <= 1)
            return;

        int pos = n - 2;
        while (pos >= 0 && nums[pos] >= nums[pos + 1]){ // 遞增,等號問題
            pos--;
        }
        if (pos >= 0){
            // 找到pos後面 比 nums[pos] 大的最小的一個數
            int j = n - 1;
            while (nums[j] <= nums[pos]){
                j--;
            }

            // swap
            int tmp = nums[j];
            nums[j] = nums[pos];
            nums[pos] = tmp;

            // reverse pos之後的所有數
            rev(nums, pos + 1, n - 1);
        }
        else{
            rev(nums, 0, n - 1);
        }
    }
};