Skip to content

Commit

Permalink
leetcode: Add index to 2485
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jul 29, 2024
1 parent 338925f commit 84883fe
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/leetcode/2485.find-the-pivot-integer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "lc-0000-name"
name = "lc-2485-find-the-pivot-integer"
version = "0.1.0"
edition = "2021"
publish = false
Expand Down
53 changes: 51 additions & 2 deletions src/leetcode/2485.find-the-pivot-integer/index.md
Original file line number Diff line number Diff line change
@@ -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)`.
7 changes: 5 additions & 2 deletions src/leetcode/2485.find-the-pivot-integer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -39,6 +39,9 @@ pub fn pivot_integer2(n: i32) -> i32 {
}
left_sum += i;
right_sum -= i;
if left_sum > right_sum {
break;
}
}
-1
}
Expand Down
1 change: 1 addition & 0 deletions src/leetcode/by-id/2401-2500.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 2401-2500
7 changes: 4 additions & 3 deletions src/leetcode/tags/prefix-sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

## 中等

Expand Down

0 comments on commit 84883fe

Please sign in to comment.