Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0afc3f7

Browse files
committed
修复0494.目标和题目Python二维DP解法中的错误
在遍历过程中,只有j >= 本次遍历元素才可以选取当前元素,原代码中错写为j>=nums[i-1],修复后变为j>=nums[i],提交才可以通过leetcode所有用例。
1 parent 7aa973b commit 0afc3f7

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

‎problems/0494.目标和.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ class Solution:
684684
for i in range(1, len(nums)):
685685
for j in range(target_sum + 1):
686686
dp[i][j] = dp[i - 1][j] # 不选取当前元素
687-
if j >= nums[i-1]:
687+
if j >= nums[i]: #只有j >= 本次遍历元素才可以选取当前元素
688688
dp[i][j] += dp[i - 1][j - nums[i]] # 选取当前元素
689689

690690
return dp[len(nums)-1][target_sum] # 返回达到目标和的方案数

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /