What's wrong with my code here? Java Simple recursive


  • 0
    V
    public class Solution {
        public int findTargetSumWays(int[] nums, int S) {
            if(nums.length == 0) return 0;
            return helper(nums, S, nums.length-1) ;
        }
        
        public int helper(int[] nums, int target,  int end){
            if(end == 0){
                if( target == nums[end] || target == -nums[end]) {  return 1;}
                else return 0;
            }
            return helper(nums, target-nums[end], end-1)+ helper(nums, target+ nums[end], end-1);
        }
    }
    
    For the case of [0,0,0,0,0,0,0,0,1]
    1 ., i get 128 insndtead of 256

Log in to reply
 

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