Skip to content

Commit 2e628eb

Browse files
committed
Merge pull request #169 from zhufenggood/master
Update solution 0494. Considering new edge test case.
2 parents 2189396 + a3f55d9 commit 2e628eb

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

leetcode/0494.Target-Sum/494. Target Sum.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ func findTargetSumWays(nums []int, S int) int {
66
for _, n := range nums {
77
total += n
88
}
9-
if S > total || (S+total)%2 == 1 {
9+
if S > total || (S+total)%2 == 1 || S+total < 0 {
1010
return 0
1111
}
1212
target := (S + total) / 2

leetcode/0494.Target-Sum/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ There are 5 ways to assign symbols to make the sum of nums be target 3.
5252

5353
等号两边都加上 `sum(N) + sum(P)`,于是可以得到结果 `2 * sum(P) = target + sum(nums)`,那么这道题就转换成了,能否在数组中找到这样一个集合,和等于 `(target + sum(nums)) / 2`。那么这题就转化为了第 416 题了。`dp[i]` 中存储的是能使和为 `i` 的方法个数。
5454

55-
- 如果和不是偶数,即不能被 2 整除,那说明找不到满足题目要求的解了,直接输出 0
55+
- 如果和不是偶数,即不能被 2 整除,或者和是负数,那说明找不到满足题目要求的解了,直接输出 0
5656

5757
## 代码
5858

@@ -63,7 +63,7 @@ func findTargetSumWays(nums []int, S int) int {
6363
for _, n := range nums {
6464
total += n
6565
}
66-
if S > total || (S+total)%2 == 1 {
66+
if S > total || (S+total)%2 == 1 || S+total < 0 {
6767
return 0
6868
}
6969
target := (S + total) / 2

0 commit comments

Comments
 (0)