-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
64 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# 2401-2500 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters