Shortest C++ code (Not the fastest though)


  • 0
    G
        int findTargetSumWays(vector<int>& nums, int S) {
            return _f(nums, 0, S);
        }
        int _f(vector<int>& nums, int i, int S) {
            if (i == nums.size()) {
                return S == 0 ? 1 : 0;
            }
            return _f(nums, i + 1, S - nums[i]) + _f(nums, i + 1, S + nums[i]);
        }
    

Log in to reply
 

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