Combination Sum

来源:互联网 发布:人工智能等级 编辑:程序博客网 时间:2024/06/16 22:39

46. Permutations

Given a collection of distinct numbers, return all possible permutations.

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

[  [1,2,3],  [1,3,2],  [2,1,3],  [2,3,1],  [3,1,2],  [3,2,1]]
void dfs_permute(vector<int> &nums, int pos, vector<vector<int>>& res){    if (pos == nums.size()){        res.push_back(nums);        return;    }    for (int i = pos; i < nums.size(); i++){        swap(nums[i], nums[pos]);        dfs_permute(nums, pos + 1, res);        swap(nums[i], nums[pos]);    }}vector<vector<int>> permute(vector<int>& nums) {    vector<vector<int>> res;    if (nums.size() == 0)return res;    dfs_permute(nums, 0, res);    return res;}

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]]

思路1:直接使用STL里面的next_permutation()函数

vector<vector<int> > permuteUnique(vector<int>& nums) {    vector<vector<int> > res;    if (nums.size() == 0)return res;    sort(nums.begin(), nums.end());    do{        res.push_back(nums);    } while (next_permutation(nums.begin(), nums.end()));    return res;}

思路2:利用回溯,但是在此过程中判断后续元素其前区间元素是否有相同元素,如果没有,则进行排列;

bool canSwap(vector<int>& nums, int begin, int end){    for (int i = begin; i < end; i++){        if (nums[i] == nums[end])return false;    }    return true;}void dfs_permuteUnique(vector<int>&nums, int pos, vector<vector<int>>& res){    if (pos == nums.size()){        res.push_back(nums);        return;    }    for (int i = pos; i < nums.size(); i++){        if (canSwap(nums, pos, i)){            swap(nums[i], nums[pos]);            dfs_permuteUnique(nums, pos + 1, res);            swap(nums[i], nums[pos]);        }    }}vector<vector<int> > permuteUnique(vector<int>& nums) {    vector<vector<int> > res;    if (nums.size() == 0)return res;    dfs_permuteUnique(nums, 0, res);    return res;}

思路3:利用一个hashmap来存储每个数字出现的次数,然后根据每个元素出现的次数进行全排列,推荐这种做法;

void dfs(vector<int>& nums, int cnt, vector<int>& path, unordered_map<int, int>& count, vector<vector<int> >& res){    if (cnt == 0){        res.push_back(path);        return;    }    for (auto it = count.begin(); it != count.end(); it++){        if (it->second != 0){            path.push_back(it->first);            it->second--;            dfs(nums, cnt - 1, path, count, res);            it->second++;            path.pop_back();        }    }}vector<vector<int> > permuteUnique(vector<int>& nums) {    vector<vector<int> > res;    if (nums.size() == 0)return res;    unordered_map<int, int> count;    for (auto x : nums)count[x]++;    vector<int> path;    dfs(nums, nums.size(), path, count, res);    return res;}

31. Next Permutation

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).

The replacement must be in-place, do not allocate extra memory.

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

1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1
void nextPermutation(vector<int>& nums) {    if (nums.size() <= 1)return;    for (int i = nums.size() - 1; i >= 0; i--){        for (int j = nums.size() - 1; j > i; j--){            if (nums[i] < nums[j]){                swap(nums[i], nums[j]);                sort(nums.begin() + i + 1, nums.end());                return;            }        }    }    sort(nums.begin(), nums.end());    return;}

60. Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations.

By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):

"123""132""213""231""312""321"

Given n and k, return the kth permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

string getPermutation(int n, int k) {    vector<int> perm(n + 1, 1);    for (int i = 1; i <= n; i++)perm[i] = perm[i - 1] * i;    int num = n - 1;    string res;    vector<char> digits = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };    while (num){        int t = (k - 1) / perm[num--];        k -= t * perm[num + 1];        res.push_back(digits[t]);        digits.erase(digits.begin() + t);    }    res.push_back(digits[k - 1]);    return res;}
原创粉丝点击