File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -706,6 +706,31 @@ class Solution:
706
706
```
707
707
708
708
### Go
709
+ 回溯法思路
710
+ ``` go
711
+ func findTargetSumWays (nums []int , target int ) int {
712
+ var result int
713
+ var backtracking func (nums []int , target int , index int , currentSum int )
714
+
715
+ backtracking = func (nums []int , target int , index int , currentSum int ) {
716
+ if index == len (nums) {
717
+ if currentSum == target {
718
+ result++
719
+ }
720
+ return
721
+ }
722
+
723
+ // 选择加上当前数字
724
+ backtracking (nums, target, index+1 , currentSum+nums[index])
725
+
726
+ // 选择减去当前数字
727
+ backtracking (nums, target, index+1 , currentSum-nums[index])
728
+ }
729
+
730
+ backtracking (nums, target, 0 , 0 )
731
+ return result
732
+ }
733
+ ```
709
734
二维dp
710
735
``` go
711
736
func findTargetSumWays (nums []int , target int ) int {
You can’t perform that action at this time.
0 commit comments