From 4eb6dfbdfea60be74b5e16764738185ad2fdb37a Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Fri, 23 Oct 2026 04:39:45 +0800 Subject: [PATCH] =?UTF-8?q?Update=20Leetcode=20=E9=A2=98=E8=A7=A3=20-=20?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Fable 5 --- notes/Leetcode 题解 - 动态规划.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/notes/Leetcode 题解 - 动态规划.md b/notes/Leetcode 题解 - 动态规划.md index b50a12e6..6f5bd372 100644 --- a/notes/Leetcode 题解 - 动态规划.md +++ b/notes/Leetcode 题解 - 动态规划.md @@ -139,7 +139,7 @@ private int rob(int[] nums, int first, int last) { ### 4. 信件错排 -题目描述:有 N 个 信 和 信封,它们被打乱,求错误装信方式的数量。 +题目描述:有 N 个信和信封,它们被打乱,求每封信都装错信封的方式数量(错排数)。 定义一个数组 dp 存储错误方式数量,dp[i] 表示前 i 个信和信封的错误方式数量。假设第 i 个信装到第 j 个信封里面,而第 j 个信装到第 k 个信封里面。根据 i 和 k 是否相等,有两种情况: @@ -158,7 +158,7 @@ private int rob(int[] nums, int first, int last) { 题目描述:假设农场中成熟的母牛每年都会生 1 头小母牛,并且永远不会死。第一年有 1 只小母牛,从第二年开始,母牛开始生小母牛。每只小母牛 3 年之后成熟又可以生小母牛。给定整数 N,求 N 年后牛的数量。 -第 i 年成熟的牛的数量为: +第 i 年牛的数量等于第 i-1 年牛的数量加上第 i-3 年牛的数量,这是因为第 i-3 年的牛到第 i 年都已成熟,每头都会生一头小牛: @@ -807,7 +807,7 @@ sum(P) + sum(N) + sum(P) - sum(N) = target + sum(P) + sum(N) ```java public int findTargetSumWays(int[] nums, int S) { int sum = computeArraySum(nums); - if (sum < S || (sum + S) % 2 == 1) { + if (sum < Math.abs(S) || (sum + S) % 2 != 0) { return 0; } int W = (sum + S) / 2;