Merge pull request #1072 from Xiaofei-fei/master

修改0494.目标和 python代码
This commit is contained in:
程序员Carl
2022-02-17 10:34:14 +08:00
committed by GitHub
2 changed files with 2 additions and 1 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@ -272,7 +272,8 @@ Python
class Solution: class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int: def findTargetSumWays(self, nums: List[int], target: int) -> int:
sumValue = sum(nums) sumValue = sum(nums)
if target > sumValue or (sumValue + target) % 2 == 1: return 0 #注意边界条件为 target>sumValue or target<-sumValue or (sumValue + target) % 2 == 1
if abs(target) > sumValue or (sumValue + target) % 2 == 1: return 0
bagSize = (sumValue + target) // 2 bagSize = (sumValue + target) // 2
dp = [0] * (bagSize + 1) dp = [0] * (bagSize + 1)
dp[0] = 1 dp[0] = 1