From 84883feb9262627b59e9b6e658be37de394ed1aa Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Mon, 29 Jul 2024 11:10:07 +0800 Subject: [PATCH] leetcode: Add index to 2485 --- src/SUMMARY.md | 2 + .../2485.find-the-pivot-integer/Cargo.toml | 2 +- .../2485.find-the-pivot-integer/index.md | 53 ++++++++++++++++++- .../2485.find-the-pivot-integer/src/main.rs | 7 ++- src/leetcode/by-id/2401-2500.md | 1 + src/leetcode/tags/prefix-sum.md | 7 +-- 6 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 src/leetcode/by-id/2401-2500.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index cc8680bbd..031917a38 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -300,6 +300,8 @@ - [2101-2200](leetcode/by-id/2101-2200.md) - [2108. 找出数组中的第一个回文字符串 Find First Palindromic String in the Array](leetcode/2108.find-first-palindromic-string-in-the-array/index.md) - [2119. 反转两次的数字 A Number After a Double Reversal](leetcode/2119.a-number-after-a-double-reversal/index.md) + - [2401-2500](leetcode/by-id/2401-2500.md) + - [2485. 找出中枢整数 Find the Pivot Integer](leetcode/2485.find-the-pivot-integer/index.md) - [2501-2600](leetcode/by-id/2501-2600.md) - [2574. 左右元素和的差值 Left and Right Sum Differences](leetcode/2574.left-and-right-sum-differences/index.md) - [2801-2900](leetcode/by-id/2801-2900.md) diff --git a/src/leetcode/2485.find-the-pivot-integer/Cargo.toml b/src/leetcode/2485.find-the-pivot-integer/Cargo.toml index 06d239521..4a9f56f94 100644 --- a/src/leetcode/2485.find-the-pivot-integer/Cargo.toml +++ b/src/leetcode/2485.find-the-pivot-integer/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "lc-0000-name" +name = "lc-2485-find-the-pivot-integer" version = "0.1.0" edition = "2021" publish = false diff --git a/src/leetcode/2485.find-the-pivot-integer/index.md b/src/leetcode/2485.find-the-pivot-integer/index.md index 845f89887..2b581b518 100644 --- a/src/leetcode/2485.find-the-pivot-integer/index.md +++ b/src/leetcode/2485.find-the-pivot-integer/index.md @@ -1,4 +1,53 @@ +# 2485. 找出中枢整数 Find the Pivot Integer -# +[问题描述](https://leetcode.com/problems/find-the-pivot-integer) -[问题描述](https://leetcode.com/problems/) +这个问题的解法就比较多了. + +## 前缀和 Prefix Sum + +根据问题中的描述, 可以直接想到的方法就是前缀和. + +处理步骤如下: + +1. 构造 `[1..=n]` 的前缀和数组 +2. 遍历前缀和数组, 如果左侧部分等于右侧部分, 就返回, `prefix * 2 == total_sum + i as i32` +3. 否则没有找到, 就返回 -1 + +代码实现如下: + +```rust +{{#include src/main.rs:5:28}} +``` + +该算法的时间复杂度是 `O(n)`, 空间复杂度是 `O(n)`. + +## 双指针法 + +步骤如下: + +1. 构造两个指针, 分别代表左侧之和与右侧之和 + 1. 左侧之和初始化为0 + 2. 右侧之和被初始化为 `n * (n + 1) /2` +2. 遍历 `1..=n`, 然后判断左侧之和与右侧之和的关系 + 1. 如果 `left_sum + i == right_sum`, 说明找到了该位置, 直接返回 + 2. 否则更新左侧之和与右侧之和 + 3. 如果 `left_sum > right_sum`, 说明没有找到合适的位置, 直接返回 -1 + +该算法的实现如下: + +```rust +{{#include src/main.rs:30:44}} +``` + +该算法的时间复杂度是 `O(n)`, 空间复杂度是 `O(1)`. + +## 平方根法 + +这个方法的原理还不太清楚, 先计录一下. + +```rust +{{#include src/main.rs:49:58}} +``` + +该算法的时间复杂度是 `O(1)`, 空间复杂度是 `O(1)`. diff --git a/src/leetcode/2485.find-the-pivot-integer/src/main.rs b/src/leetcode/2485.find-the-pivot-integer/src/main.rs index 23f32d109..6ca4b3d44 100644 --- a/src/leetcode/2485.find-the-pivot-integer/src/main.rs +++ b/src/leetcode/2485.find-the-pivot-integer/src/main.rs @@ -17,9 +17,9 @@ pub fn pivot_integer1(n: i32) -> i32 { let total_sum = prefix_sum[len - 1]; // 遍历前缀和数组 - for i in 0..len { + for (i, prefix) in prefix_sum.into_iter().enumerate() { // 满足左侧之和等于右侧之后 - if prefix_sum[i] * 2 == total_sum + i as i32 { + if prefix * 2 == total_sum + i as i32 { return i as i32; } } @@ -39,6 +39,9 @@ pub fn pivot_integer2(n: i32) -> i32 { } left_sum += i; right_sum -= i; + if left_sum > right_sum { + break; + } } -1 } diff --git a/src/leetcode/by-id/2401-2500.md b/src/leetcode/by-id/2401-2500.md new file mode 100644 index 000000000..4775cf06f --- /dev/null +++ b/src/leetcode/by-id/2401-2500.md @@ -0,0 +1 @@ +# 2401-2500 \ No newline at end of file diff --git a/src/leetcode/tags/prefix-sum.md b/src/leetcode/tags/prefix-sum.md index 3f8238867..e55469868 100644 --- a/src/leetcode/tags/prefix-sum.md +++ b/src/leetcode/tags/prefix-sum.md @@ -8,9 +8,10 @@ 4. [1480. 一维数组的动态和 Running Sum of 1d Array](../1480.running-sum-of-1d-array/index.md) 5. [1732. 找到最高海拔 Find the Highest Altitude](../1732.find-the-highest-altitude/index.md) 6. [1991. 找到数组的中间位置 Find the Middle Index in Array](../1991.find-the-middle-index-in-array/index.md) -7. [2574. 左右元素和的差值 Left and Right Sum Differences](../2574.left-and-right-sum-differences/index.md) -8. [2848. 与车相交的点 Points That Intersect With Cars](../2848.points-that-intersect-with-cars/index.md) -9. [3028. 边界上的蚂蚁 Ant on the Boundary](../3028.ant-on-the-boundary/index.md) +7. [2485. 找出中枢整数 Find the Pivot Integer](../2485.find-the-pivot-integer/index.md) +8. [2574. 左右元素和的差值 Left and Right Sum Differences](../2574.left-and-right-sum-differences/index.md) +9. [2848. 与车相交的点 Points That Intersect With Cars](../2848.points-that-intersect-with-cars/index.md) +10. [3028. 边界上的蚂蚁 Ant on the Boundary](../3028.ant-on-the-boundary/index.md) ## 中等