Clean Java backtracking solution


  • 1
    Y
    public class Solution {
        int count = 0;
        
        public int findTargetSumWays(int[] nums, int S) {
            helper(nums, S, 0);
            return count;
        }
        
        public void helper(int[] nums, int S, int start) {
            if(start == nums.length) {
                if(S == 0) count ++;
                return;
            }
            helper(nums, S - nums[start], start + 1);
            helper(nums, S + nums[start], start + 1);
        }
    }
    

Log in to reply
 

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