Simple C recursive solution, 402ms


  • 0
    L
    int helper(int *begin, int *end, int S) {
        if (end - begin == 1) {
            return (*begin == S) + (*begin == -S);
        }
        return helper(begin + 1, end, S - *begin) + helper(begin + 1, end, S + *begin);
    }
    int findTargetSumWays(int* nums, int numsSize, int S) {
        return helper(nums, nums + numsSize, S);
    }
    

Log in to reply
 

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