clueless coding // TODO: be smarter

LeetCode Permutations II: CPP Solution



TL;DR Code Solution



void permute(vector<vector<int>>& result, vector<int> nums, int index) {
    if (index == (int)nums.size()) {
        result.push_back(nums);
        return;
    } 

    for (int i = index; i < nums.size(); i++) {
        if (i != index && nums[index] == nums[i]) continue;
        swap(nums[i], nums[index]);
        permute(result, nums, index + 1);
    }
}

vector<vector<int>> permuteUnique(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    vector<vector<int>> result;
    vector<int> current;
    permute(result, nums, 0);
    return result;
}