Java Concise Recursion Solution


  • 0
    Z
    public class Solution {
        public int findTargetSumWays(int[] nums, int S) {
            return findWays(nums, 0, S);
        }
        
        public int findWays(int[] nums, int start, int S) {
            int n = nums.length;
            if (start >= nums.length) {
                return S == 0 ? 1 : 0;
            }
            return findWays(nums, start + 1, S - nums[start]) + findWays(nums, start + 1, S + nums[start]);
        }
        
    
    }
    

Log in to reply
 

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