who can tell me what's wrong is it in my code..

  • 0

    I passed almost every case,but failed at [100,100....],which is a int[100],and every element in it is 100.

    public class Solution {
        public boolean canPartition(int[] nums) {
            if(nums.length==0)return false;
            int sum=0;
            for(int n:nums){
                sum += n;
            if(sum%2==1)return false;
            int[] dp = new int[sum/2+1];
            dp[0] = 1;
            for(int i = 1;i<nums.length;i++){
                for(int j = sum/2;j>=nums[i-1];j--){
                    dp[j] += dp[j-nums[i-1]];
            return dp[sum/2]>0;

  • 1

    I know,int is too short for this case,so when j get bigger and bigger,it overflows

Log in to reply

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