just can not pass,can you figure it out?


  • 0
    H

    this is my code ,in my IDE vs2012,win32 the instance
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,90};
    output is true;
    but online judge is false?
    why?

    bool canPartition(vector<int> & nums) {
    int sum=0;
    int n=nums.size();

    for(int i=0;i<n;i++){
    	sum+=nums[i];
    }
    if(sum%2==1)
    	return false;
    
    
    int half=sum/2;
    int  **dp=new int* [n+1];
    for(int i=0;i<=n;i++){
    	dp[i]=new int [half+1];
    }
    dp[0][0]=0;
    for(int j=nums[0];j<=half;j++){
    	dp[0][j]=nums[0];
    }
    for(int i=1;i<n;i++)
    	for(int j=nums[i];j<=half;j++){
    		dp[i][j]=(dp[i-1][j]>dp[i-1][j-nums[i]]+nums[i])
    			?dp[i-1][j]:dp[i-1][j-nums[i]]+nums[i];
    	}
    
    if(dp[n-1][half]==half)
    	return true;
    else return false;
    

    }


Log in to reply
 

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