Coding-Interview-101

Solutions to LeetCode problems filtered with companies, topics and difficulty.

View project on GitHub

Permutations


Solution


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