short java dfs solution


  • 0
    L
    public int findTargetSumWays(int[] nums, int S) {
            if (nums == null || nums.length == 0) return 0;
            return dfs(nums,S,0,0);
        }
        
        private int dfs(int[] nums, int S, int sum, int index) {
            if (index == nums.length) {
                if (sum == S) {
                    return 1;
                } else {
                    return 0;
                }
            }
            return dfs(nums,S,sum+nums[index],index+1) + dfs(nums,S,sum-nums[index],index+1);
        }
    

Log in to reply
 

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