My Java solution


  • 2
        public int findTargetSumWays(int[] nums, int S) {
            if (nums == null || nums.length == 0){
                return 0;
            }
            return helper(nums, S, 0, 0, 0);
        }
        
        public int helper(int[] nums, int S, int sum, int index, int count){
            if (index == nums.length){
                if (sum == S){
                    count ++;
                }
                return count;
            }
            return helper(nums, S, sum + nums[index], index + 1, count) + helper(nums, S, sum - nums[index], index + 1, count);
        }
    

Log in to reply
 

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