class Solution { public: int findTargetSumWays(vector<int>& nums, int S) { return cal(0,0,nums,S); } int cal(int index, int sum, vector<int> &nums, int S){ if (index==nums.size()){ if (sum==S) return 1; else return 0; } return cal(index+1,sum+nums[index],nums,S)+cal(index+1,sum-nums[index],nums,S); } };
原文:https://www.cnblogs.com/hankunyan/p/9114527.html