Why my code get correct results on Mac but get error on Leetcode OJ


  • 0
    Y

    on mac when input is:

    Input: [2,1,3,1,4], 10

    it returned [[1,2,3,4]]

    but on Leetcode it returned:

    Output: [[1,2,3,4],[1,2,3,4],[1,1,4,4],[2,4,4]]

    ?????

    
    void search(vector<int>& num, int curr_idx, int curr_sum, int target, vector<int>& temp, vector<vector<int> >& res){
        if(curr_sum == target){
            res.push_back(temp);
            return;
        }
        if(curr_sum > target || curr_idx > num.size()) return;
        int n = curr_idx+1;
        while(n<num.size() && num[n] == num[n-1]) n++;
        vector<int> x = temp;
        for(int i=curr_idx; i<n; i++){
            temp.push_back(num[curr_idx]);
            search(num, n, curr_sum+(i-curr_idx+1)*num[curr_idx], target, temp, res);
        }
        temp = x;
        search(num, n, curr_sum, target, temp, res);
    }
    
    vector<vector<int> > combinationSum2(vector<int>& candidates, int target) {
        vector<vector<int> > res;
        if(candidates.size() == 0) return res;
        std::sort(candidates.begin(), candidates.end());
        vector<int> temp;
        search(candidates,0,0,target,temp,res);
        return res;
    }
    

Log in to reply
 

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