leetcode-master icon indicating copy to clipboard operation
leetcode-master copied to clipboard

0494.目标和 回溯算法你写得不好,这么写不会超时

Open liyunlongaaa opened this issue 3 years ago • 2 comments

class Solution { public: int ans = 0; void dfs(vector<int> &nums, int target, int curSum, int Idx, int len) { if (Idx == len) { if (curSum == target) ans++; return ; } dfs(nums, target, curSum + nums[Idx], Idx + 1, len); dfs(nums, target, curSum - nums[Idx], Idx + 1, len); } int findTargetSumWays(vector<int>& nums, int target) { int len = nums.size(); dfs(nums, target, 0, 0, len); return ans; } };

liyunlongaaa avatar Jan 14 '22 02:01 liyunlongaaa

用下 md 文档吧,这样,也看不出来

LightSeekr avatar Mar 21 '22 02:03 LightSeekr

这是啥呀?

0ws0 avatar Oct 09 '23 03:10 0ws0