Why this solution does not work?


  • 0
    J

    I use the following method to remove duplicates instead of a set or hashmap. But it can't AC. Can someone explain me why this is wrong? Better with an example. Thanks!

    void dfs(vector<int>& nums, int curr, vector<int>& path, vector<vector<int> >& result) {
            if (path.size() >= 2) {
                result.push_back(path);
            }
            if (curr == nums.size())
                return;
    
            int last; 
            for (int i = curr; i < nums.size(); i++) {
                if (i > curr && nums[i] == last) continue; //to avoid adding the same element in the same level
                if (path.size() == 0 || nums[i] >= path.back()) {
                    path.push_back(nums[i]);
                    last = nums[i];
                    dfs(nums, i+1, path, result);
                    path.pop_back();
                }
            }
        }
        
        vector<vector<int>> findSubsequences(vector<int>& nums) {
            vector<int> path;
            vector<vector<int> > result;
            
            if (nums.empty() || nums.size() <= 1)
                return result;
                
            dfs(nums, 0, path, result);
            return result;
        }
    

Log in to reply
 

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