YunheWang0813
9/19/2019 - 12:15 AM

0046. Permutations

class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        if (nums.empty() || nums.size() == 0) return res;
        vector<int> list;
        helper(list, nums);
        return res;
    }
    void helper(vector<int>& list, vector<int>& nums) {
        if (list.size() == nums.size()) {
            res.push_back(list);
            return;
        }
        for (int i = 0; i < nums.size(); i++) {
            auto p = find(list.begin(), list.end(), nums[i]);   // O(N)
            if (p != list.end()) continue;
            list.push_back(nums[i]);
            helper(list, nums);
            list.erase(list.end() - 1);   // backtrack push & delete
        }
    }
private:
    vector<vector<int>> res;
};
class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        if (nums.empty() || nums.size() == 0) return res;
        helper(0, nums);
        return res;
    } 
    void helper(int count, vector<int>& nums) {
        if (count == nums.size()) {
            vector<int> list(nums.size());
            for (int i = 0; i < nums.size(); i++) {
                list[i] = nums[i];
            }
            res.push_back(list);
            return;
        }
        for (int i = count; i < nums.size(); i++) {
            swap(nums[count], nums[i]);
            helper(count + 1, nums);
            swap(nums[count], nums[i]);
        }
    }
private:
    vector<vector<int>> res;
};
class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        if (nums.empty() || nums.size() == 0) return res;
        vector<int> cur;
        helper(nums, cur);
        return res;
    }
    void helper(vector<int>& nums, vector<int>& cur) {
        if (cur.size() == nums.size()) res.push_back(cur);
        for (int i = 0; i < nums.size(); i++) {
            auto it = find(cur.begin(), cur.end(), nums[i]);
            if (it != cur.end()) continue;
            cur.push_back(nums[i]);
            helper(nums, cur);
            cur.pop_back();
        }
    }
private:
    vector<vector<int>> res;
};
class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        if (nums.size() == 0) return res;
        vector<int> comb;
        helper(nums, comb);
        return res;
    }
    void helper(vector<int>& nums, vector<int>& comb) {
        if (nums.size() == comb.size()) {
            res.push_back(comb);
            return;
        }
        for (int i = 0; i < nums.size(); i++) {
            auto it = find(comb.begin(), comb.end(), nums[i]);
            if (it != comb.end()) continue;
            comb.push_back(nums[i]);
            helper(nums, comb);
            comb.pop_back();
        }
    }
private:
    vector<vector<int>> res;
};

算法

用什么:Backtracking

为什么:Backtracking典型题,找到所有组合

解题

思路:

  • 用find的做法
  1. 创造一个helper function,把vector作为新的参数,初始为空
  2. base case:list的item数满了(==nums的数量),把该list作为一个答案,push到res里
  3. main recursion:为了避免重复,先看list里有没有与当前for loop得到要素一致的要素,如果不是就放到list里然后再call helper,然后别忘记删掉push的要素
  • 用swap的做法
  1. 创造一个helper function,把int作为新的参数,初始为0
  2. base case:count==nums.size()时,拷贝当前order的nums,并作为一种答案加入res里
  3. main recursion:用swap把count和i的index指的要素调换,call helper(count+1),再swap回去

复杂度

时间:

  • find法: O(n!),因为find,实际上是O(n! * n)
  • swap法: O(n!)

这种形式的递归大多都是O(n!)

空间:O(n)

类似的题

47