2024-06-30发表2025-01-15更新LeetCode每日一题几秒读完 (大约95个字)0次访问494. 目标和目标和 难度: medium 原始链接: https://leetcode.cn/problems/target-sum 标签: 深度优先遍历 解法一: 深度优先遍历go123456789101112131415161718func findTargetSumWays(nums []int, target int) int { // f(i) = nums[i] + f(i - 1) // f(i) = -nums[i] + f(i - 1) ans := 0 var dfs func(int, int) dfs = func(i, cur int) { if i < 0 { if cur == 0 { ans++ } return } dfs(i-1, cur+nums[i]) dfs(i-1, cur-nums[i]) } dfs(len(nums)-1, target) return ans} 494. 目标和https://wuhunyu.top/leetcode/2024/06/target-sum/index.html作者wuhunyu发布于2024-06-30更新于2025-01-15许可协议#深度优先遍历