38 ms solution DP


  • 0
    D

    public class Solution {
    public boolean canPartition(int[] nums) {
    int sum=0;
    for(int i:nums)
    {
    sum+=i;
    }
    if(sum%2!=0)
    return false;
    else
    {
    sum=sum/2;

    		boolean dp[]= new boolean[sum+1];
    		dp[0]=true;
    		for(int i:nums){
    			
    			for(int j=sum;j>=i;j--)
    			{
    				dp[j]=dp[j]||dp[j-i];
    			}
    		}
    		return dp[sum];
    		
    		
    	}
    }
    

    }


Log in to reply
 

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