Coding-Interview-101

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

View project on GitHub

Permutations II


Solution


   class Solution {
    public:
        bool check(vector<int>& nums, int idx, int curr) {
            for (int i = idx; i < curr; i++) 
                if (nums[i] == nums[curr])
                    return 0;
            return 1;
        }
        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++) {
                    if(check(nums, idx, i)) {
                        swap(nums[i], nums[idx]);
                        perm(ans, nums, idx + 1);
                        swap(nums[i], nums[idx]);
                    }
                }
            }
        }
        vector<vector<int>> permuteUnique(vector<int>& nums) {
            vector<vector<int>> ans;
            perm(ans, nums, 0);
            return ans;
        }
    };