leetcode排列組合集合

46. Permutations
Given a collection of distinct integers, return all possible permutations.

Example:

Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

class Solution {
public:
    vector<vector<int>> outer;
    vector<vector<int>> permute(vector<int>& nums) {
        Permute(nums, 0);
        return outer;
    }
    void Permute(vector<int>& nums, int i){
        if(i>=nums.size()){
            outer.push_back(nums);
            return;
        }
        for(int index = i; index<nums.size(); index++){
            swap(nums[i], nums[index]);
            Permute(nums, i+1);
            swap(nums[i], nums[index]);
        }
    }
};

47. Permutations II

78. Subsets

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]


class Solution {
public:
    vector<vector<int>> outer;
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<int> v;
        permute(v, 0, nums);
        return outer;
}
    void permute(vector<int>& v,  int i, vector<int>& nums){
        outer.push_back(v);
        if(i>=nums.size())
            return;
        for(int index=i; index<nums.size(); index++){
            v.push_back(nums[index]);
            permute(v, index+1, nums);
            v.pop_back();
        }
    }
};

90. Subsets II
39. Combination Sum
(移動研究院筆試題)
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

The same repeated number may be chosen from candidates unlimited number of times.

Note:

All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:

Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:

Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]

class Solution {
public:
    vector<vector<int>> outer;
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<int> v;
        permute(candidates, v, target, 0);
        return outer;
    }
    void permute(vector<int>& can, vector<int>& v, int target, int index){
        if(index>=can.size() || target<0)
            return;
        if(target==0){
            outer.push_back(v);
            return;
        }
        for(int i=index; i<can.size(); i++){
            v.push_back(can[i]);
            permute(can, v, target-can[i], i);
            v.pop_back();
        }
    }
};

40. Combination Sum II

Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

Each number in candidates may only be used once in the combination.

Note:

All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:

Input: candidates = [10,1,2,7,6,1,5], target = 8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
Example 2:

Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
[1,2,2],
[5]
]

class Solution {
public:
    vector<vector<int>> outer;
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<int> v;
        sort(candidates.begin(), candidates.end());
        permute(candidates, 0, v, target);
        return outer;
    }
    void permute(vector<int>& can, int index, vector<int>& v, int target){
        if(target==0){
            outer.push_back(v);
            return;
        }
        if(target<0 || index>=can.size())
            return;
        
        for(int i=index; i<can.size(); i++){
            if(i>0 && can[i]==can[i-1] && i!=index)
                continue;
            v.push_back(can[i]);
            permute(can, i+1, v, target-can[i]);
            v.pop_back();
        }
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章