I think this question should be in "medium" level


  • 0
    W

    Below is my solution for this problem.

    It has only one thing different from Medium level problem "Permutations". I commented that two lines. I don't think these two lines make this problem from Medium to Hard.

    Besides, my solution to Permuations was actually this code because I thought "collection" would include duplicate entries.

    void permuteHelper(vector<int>& nums, vector<vector<int>>& result, vector<int>& partResult)
    {
        if (nums.empty())
        {
            result.push_back(partResult);
            return;
        }
        
        for (int i = 0; i < nums.size(); i++)
        {
            int num = nums.at(i);
            partResult.push_back(num);
            nums.erase(nums.begin() + i);
            
            permuteHelper(nums, result, partResult);
            
            partResult.pop_back();
            nums.insert(nums.begin() + i, num);
            
            // These two lines are the only thing that's different from the solution for Permutations problem
            while (i < nums.size()-1 && nums.at(i) == nums.at(i+1))
                i++;
        }
    }
    
    vector<vector<int>> permute(vector<int>& nums) 
    {
        vector<vector<int>> result;
        if (nums.empty()) return result;
        
        sort(nums.begin(), nums.end());
        
        vector<int> partResult;
        permuteHelper(nums, result, partResult);
        
        return result;
    }

  • 0

    Thanks, just changed it to "Medium".


  • 0
    W

    awesome!! Thanks


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.