47. Permutations II

47. Permutations II_第1张图片
image.png
class Solution {
public:
    vector> permuteUnique(vector& nums) {
        vector> result;
        if(nums.size() == 0) return result;
        vector list;
        vector used(nums.size(), false);
        sort(nums.begin(), nums.end());
        permuteUniqueHelper(result, list, used, nums);
        return result;
    }
private:
    void permuteUniqueHelper(vector>& result, vector& list, vector& used, vector nums){
        if(list.size() == nums.size()){
            result.push_back(list);
            return;
        }
        for(int i = 0; i < nums.size(); i++){
            if(used[i] || (i != 0 && nums[i] == nums[i-1] && used[i-1] == false)){
                continue;
            }
            used[i] = true;
            list.push_back(nums[i]);
            permuteUniqueHelper(result, list, used, nums);
            list.pop_back();
            used[i] = false;
        }
    }
};

你可能感兴趣的:(47. Permutations II)