6 Lines Body C++ Recursion


  • 0
    class Solution {
    public:
        int findTargetSumWays(vector<int>& nums, int S, int current_sum = 0, int index = 0) {
            if(index == nums.size()){
                if(current_sum == S) res++;
            }else{
                findTargetSumWays(nums, S, current_sum + nums[index], index+1);
                findTargetSumWays(nums, S, current_sum - nums[index], index+1);
            }
            return res;
        }
    private:
        int res = 0;
    };
    

Log in to reply
 

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