Enumeration. Store all possible results calculated before.


  • 0
    C
    public boolean canPartition(int[] nums) {
        if(nums.length<=1) return false;
        int sum=0,target,cur=0,j=0;
        for(Integer temp:nums) sum+=temp;
        if(sum%2==1) return false;
        target=sum/2;
        HashSet<Integer> pre;
        HashSet<Integer> next=new HashSet<Integer>();
        for(int i=0;i<nums.length;i++){
            pre=new HashSet(next);
            for(Integer temp:pre) next.add(nums[i]+temp);
            next.add(nums[i]);
            if(next.contains(target)) return true;
        }
        return false;
    }

Log in to reply
 

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