Easy to understand JAVA Solution


  • 0
    C

    public class Solution {
    public boolean canPartition(int[] nums) {
    if (nums.length==0)return false;
    int sum=0;
    for(int sb:nums){
    sum+=sb;
    }
    if (sum%2!=0) return false;
    sum=sum/2;
    return helper(nums,0,sum);}
    public boolean helper(int []nums, int start,int target){
    if (target<=0)return target==0;
    boolean flag=false;
    for(int i=start;i<nums.length;i++){
    flag|=helper(nums,i+1,target-nums[i]);
    if (flag==true)return true;
    }
    return false;
    }
    }


Log in to reply
 

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