DFS recursive c++


  • 0
    J
    class Solution {
    public:
        int findTargetSumWays(vector<int>& nums, int S) {
            int mysum = 0;    
            return estimateSum(0, nums, 1, S, mysum) + estimateSum(0, nums, -1, S, mysum);
        }
        
        int estimateSum(int k, vector<int>& nums, int op, int S, int mysum) {
            mysum += op * nums[k];
            if (k == nums.size()-1 && mysum == S) return 1;
            else if (k == nums.size()-1 && mysum != S) return 0;
            else if (k < nums.size()-1) return estimateSum(k+1, nums, 1, S, mysum) + estimateSum(k+1, nums, -1, S, mysum);
        }
    };
    

Log in to reply
 

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