Help! Dp C++ version works in vc6.0 but runtime error [2,3] 5 in leetcode. Why?


  • 0
    A
    class Solution {
    public:
        vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
            
            sort(candidates.begin(), candidates.end());   
            vector<vector<vector<int> > > dp;
            for(int i = 1; i <= target; i++)   //i = 1 for all positive
            {
                vector<vector<int> > combs;
                for(int j = 0; j < candidates.size() &&  candidates[j] <= i; j++)
                {
                    if(candidates[j] == i)
                    {
                        vector<int> v;
                        v.push_back(candidates[j]);
                        combs.push_back(v);
                        break;
    					
                           
                    }
    		    
                    int t = 0;
                    for(vector<int> v = dp[i - candidates[j] - 1][t]; t < dp[i - candidates[j] - 1].size(); t++)
                    {
                        if(!v.empty() && candidates[j] <= v[0])
                        {
                            vector<int> tmp;
                            tmp.push_back(candidates[j]);
                            tmp.insert(tmp.end(), v.begin(), v.end());
                            combs.push_back(tmp);
                        }
                    }		
                        
                }
                dp.push_back(combs);
                
            }
            return dp[target - 1];
            }    
    };

Log in to reply
 

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