Wrong answer? Could you tell me the reason,thanks a lot!


  • 0
    W

    The solution below.when i test [1,2] with target=5,the result is null and it`s correct,but when i submit the result is wrong! Can someone tell me why?
    0_1473052293375_捕获2.PNG

    class Solution {
    public:
        vector<vector<int>>rlt;
        vector<int>can;
        int t;
        vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
            vector<int> nums;
            can=candidates;
            t=can.size()-1;
            sort(can.begin(),can.end());
            run(0,nums,target);
            return rlt;
        }
        void run(int i,vector<int>nums,int target){
            if(target<0) return;
            else if(target==0) {
                rlt.push_back(nums);
                return;
            }
            if(i>t) return;
            if(target<can[i]) return;
            int j=i;
            for(;can[j]==can[i];j++){}  
    //from i to j the candidate is same,we just push back all kind of different sum
            for(;i<j;i++){  
                nums.insert(nums.end(),j-i,can[i]);
                //nums.push_back(can[i]);
                run(j,nums,target-(j-i)*can[i]);
                //nums.pop_back();
               nums.erase(nums.end()-(j-i),nums.end());
            }
            
            
            run(j,nums,target);
           return;
        }
    };
    
    

Log in to reply
 

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