Short c++ recursive solution


  • 0
    S

    My solution:

    class Solution {
    public:
        int findTargetSumWays(vector<int> const& nums, int s, int i = 0)
        {
            if(i == nums.size())
                return s == 0;
            
            return findTargetSumWays(nums, s-nums[i], i+1)
                + findTargetSumWays(nums, s+nums[i], i+1);
        }
    };
    

Log in to reply
 

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