diff --git a/codes/javascript/chapter_searching/binary_search_edge.js b/codes/javascript/chapter_searching/binary_search_edge.js index 179543235..515c75b66 100644 --- a/codes/javascript/chapter_searching/binary_search_edge.js +++ b/codes/javascript/chapter_searching/binary_search_edge.js @@ -38,7 +38,7 @@ function binarySearchRightEdge(nums, target) { i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中 } } - if (j == nums.length || nums[j] != target) { + if (j < 0 || nums[j] != target) { return -1; // 未找到目标元素,返回 -1 } return j; diff --git a/codes/python/chapter_searching/binary_search_edge.py b/codes/python/chapter_searching/binary_search_edge.py index 98fcd9582..83e6f1c98 100644 --- a/codes/python/chapter_searching/binary_search_edge.py +++ b/codes/python/chapter_searching/binary_search_edge.py @@ -32,7 +32,7 @@ def binary_search_right_edge(nums: list[int], target: int) -> int: j = m - 1 # target 在区间 [i, m-1] 中 else: i = m + 1 # 首个大于 target 的元素在区间 [m+1, j] 中 - if j == len(nums) or nums[j] != target: + if j < 0 or nums[j] != target: return -1 # 未找到目标元素,返回 -1 return j diff --git a/codes/typescript/chapter_searching/binary_search_edge.ts b/codes/typescript/chapter_searching/binary_search_edge.ts index c46bcee9c..95f819c60 100644 --- a/codes/typescript/chapter_searching/binary_search_edge.ts +++ b/codes/typescript/chapter_searching/binary_search_edge.ts @@ -36,7 +36,7 @@ function binarySearchRightEdge(nums: number[], target: number): number { i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中 } } - if (j == nums.length || nums[j] != target) { + if (j < 0 || nums[j] != target) { return -1; // 未找到目标元素,返回 -1 } return j; diff --git a/docs/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_constraint_state_transfer.png b/docs/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_constraint_state_transfer.png index 3992b6f35..52713336c 100644 Binary files a/docs/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_constraint_state_transfer.png and b/docs/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_constraint_state_transfer.png differ diff --git a/docs/chapter_dynamic_programming/intro_to_dynamic_programming.md b/docs/chapter_dynamic_programming/intro_to_dynamic_programming.md index 3b2e04178..16d320292 100644 --- a/docs/chapter_dynamic_programming/intro_to_dynamic_programming.md +++ b/docs/chapter_dynamic_programming/intro_to_dynamic_programming.md @@ -661,8 +661,6 @@ $$ - 当 $j$ 等于 $1$ ,即上一轮跳了 $1$ 阶时,这一轮只能选择跳 $2$ 阶; - 当 $j$ 等于 $2$ ,即上一轮跳了 $2$ 阶时,这一轮可选择跳 $1$ 阶或跳 $2$ 阶; -![考虑约束下的递推关系](intro_to_dynamic_programming.assets/climbing_stairs_constraint_state_transfer.png) - 在该定义下,$dp[i, j]$ 表示状态 $[i, j]$ 对应的方案数。由此,我们便能推导出以下的状态转移方程: $$ @@ -672,6 +670,8 @@ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2] \end{cases} $$ +![考虑约束下的递推关系](intro_to_dynamic_programming.assets/climbing_stairs_constraint_state_transfer.png) + 最终,返回 $dp[n, 1] + dp[n, 2]$ 即可,两者之和代表爬到第 $n$ 阶的方案总数。 === "Java" diff --git a/docs/chapter_introduction/algorithms_are_everywhere.md b/docs/chapter_introduction/algorithms_are_everywhere.md index dafdb397e..c639d81ff 100644 --- a/docs/chapter_introduction/algorithms_are_everywhere.md +++ b/docs/chapter_introduction/algorithms_are_everywhere.md @@ -2,7 +2,7 @@ 当我们听到“算法”这个词时,很自然地会想到数学。然而实际上,许多算法并不涉及复杂数学,而是更多地依赖于基本逻辑,这些逻辑在我们的日常生活中处处可见。 -在正式探讨算法之前,有一个有趣的事实值得分享:**实际上,你已经学会了许多算法,并习惯将他们应用到日常生活中了**。下面,我将举两个具体例子来证实这一点。 +在正式探讨算法之前,有一个有趣的事实值得分享:**你已经在不知不觉中学会了许多算法,并习惯将它们应用到日常生活中了**。下面,我将举即个具体例子来证实这一点。 **例一:拼装积木**。一套积木,除了包含许多零件之外,还附有详细的组装说明书。我们按照说明书一步步操作,就能组装出精美的积木模型。