clueless coding // TODO: be smarter

LeetCode Permutations: CPP Solution



TL;DR Code Solution



void permute(vector<vector<int>>& result, vector<int>& nums, int index) {

    if (index == nums.size()) {
        result.push_back(nums);
        return;
    }

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

}

vector<vector<int>> permute(vector<int>& nums) {
    vector<vector<int>> result;
    permute(result, nums, 0);
    return result;
}