Java Backtracking Solution


  • 0
    S

    public class Solution {
    public int findTargetSumWays(int[] nums, int S) {
    return back(nums,S,0,0,0);
    }
    public int back(int[] nums,int S,int sum,int k,int count){
    if(k==nums.length){
    if(sum==S){
    count++;
    }
    return count;
    }
    else{
    int a=0;
    int b=0;
    a=back(nums,S,sum+nums[k],k+1,count);
    b=back(nums,S,sum-nums[k],k+1,count);
    return a+b;
    }

       }
    

    }


Log in to reply
 

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