Short 3-line C++ DFS solution


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

Log in to reply
 

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