2023-02-08 15:20:18 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
# 2.4. 权衡时间与空间
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
理想情况下,我们希望算法的时间复杂度和空间复杂度都能达到最优。然而在实际情况中,同时优化时间复杂度和空间复杂度通常是非常困难的。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
**降低时间复杂度通常需要以提升空间复杂度为代价,反之亦然**。我们将牺牲内存空间来提升算法运行速度的思路称为「以空间换时间」;反之,则称之为「以时间换空间」。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
选择哪种思路取决于我们更看重哪个方面。在大多数情况下,时间比空间更宝贵,因此以空间换时间通常是更常用的策略。当然,在数据量很大的情况下,控制空间复杂度也是非常重要的。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 2.4.1. 示例题目 *
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
以 LeetCode 全站第一题 [两数之和](https://leetcode.cn/problems/two-sum/) 为例。
|
|
|
|
|
|
|
|
|
|
!!! question "两数之和"
|
|
|
|
|
|
|
|
|
|
给定一个整数数组 `nums` 和一个整数目标值 `target` ,请你在该数组中找出“和”为目标值 `target` 的那两个整数,并返回它们的数组下标。
|
2023-04-07 22:32:04 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
|
2023-04-07 22:32:04 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
你可以按任意顺序返回答案。
|
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
「暴力枚举」和「辅助哈希表」分别对应“空间最优”和“时间最优”的两种解法。遵循时间比空间更宝贵的原则,后者是本题的最佳解法。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
### 方法一:暴力枚举
|
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
考虑直接遍历所有可能的组合。通过开启一个两层循环,判断两个整数的和是否为 `target` ,若是,则返回它们的索引(即下标)。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="leetcode_two_sum.java"
|
|
|
|
|
/* 方法一:暴力枚举 */
|
2023-02-08 16:47:52 +08:00
|
|
|
|
int[] twoSumBruteForce(int[] nums, int target) {
|
|
|
|
|
int size = nums.length;
|
|
|
|
|
// 两层循环,时间复杂度 O(n^2)
|
|
|
|
|
for (int i = 0; i < size - 1; i++) {
|
|
|
|
|
for (int j = i + 1; j < size; j++) {
|
|
|
|
|
if (nums[i] + nums[j] == target)
|
|
|
|
|
return new int[] { i, j };
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
return new int[0];
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="leetcode_two_sum.cpp"
|
|
|
|
|
/* 方法一:暴力枚举 */
|
2023-02-08 16:47:52 +08:00
|
|
|
|
vector<int> twoSumBruteForce(vector<int>& nums, int target) {
|
|
|
|
|
int size = nums.size();
|
|
|
|
|
// 两层循环,时间复杂度 O(n^2)
|
|
|
|
|
for (int i = 0; i < size - 1; i++) {
|
|
|
|
|
for (int j = i + 1; j < size; j++) {
|
|
|
|
|
if (nums[i] + nums[j] == target)
|
|
|
|
|
return { i, j };
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
return {};
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="leetcode_two_sum.py"
|
2023-03-23 18:56:56 +08:00
|
|
|
|
def two_sum_brute_force(nums: list[int], target: int) -> list[int]:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""方法一:暴力枚举"""
|
2023-02-08 16:47:52 +08:00
|
|
|
|
# 两层循环,时间复杂度 O(n^2)
|
|
|
|
|
for i in range(len(nums) - 1):
|
|
|
|
|
for j in range(i + 1, len(nums)):
|
|
|
|
|
if nums[i] + nums[j] == target:
|
2023-03-12 18:46:03 +08:00
|
|
|
|
return [i, j]
|
2023-02-08 16:47:52 +08:00
|
|
|
|
return []
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="leetcode_two_sum.go"
|
2023-02-09 04:43:12 +08:00
|
|
|
|
/* 方法一:暴力枚举 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func twoSumBruteForce(nums []int, target int) []int {
|
|
|
|
|
size := len(nums)
|
|
|
|
|
// 两层循环,时间复杂度 O(n^2)
|
|
|
|
|
for i := 0; i < size-1; i++ {
|
|
|
|
|
for j := i + 1; i < size; j++ {
|
|
|
|
|
if nums[i]+nums[j] == target {
|
|
|
|
|
return []int{i, j}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="leetcode_two_sum.js"
|
2023-02-08 16:47:52 +08:00
|
|
|
|
/* 方法一:暴力枚举 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
function twoSumBruteForce(nums, target) {
|
|
|
|
|
const n = nums.length;
|
|
|
|
|
// 两层循环,时间复杂度 O(n^2)
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
for (let j = i + 1; j < n; j++) {
|
|
|
|
|
if (nums[i] + nums[j] === target) {
|
|
|
|
|
return [i, j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="leetcode_two_sum.ts"
|
2023-02-08 16:47:52 +08:00
|
|
|
|
/* 方法一:暴力枚举 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
function twoSumBruteForce(nums: number[], target: number): number[] {
|
|
|
|
|
const n = nums.length;
|
|
|
|
|
// 两层循环,时间复杂度 O(n^2)
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
for (let j = i + 1; j < n; j++) {
|
|
|
|
|
if (nums[i] + nums[j] === target) {
|
|
|
|
|
return [i, j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="leetcode_two_sum.c"
|
2023-02-11 18:21:44 +08:00
|
|
|
|
[class]{}-[func]{twoSumBruteForce}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="leetcode_two_sum.cs"
|
2023-02-08 22:16:25 +08:00
|
|
|
|
/* 方法一:暴力枚举 */
|
|
|
|
|
int[] twoSumBruteForce(int[] nums, int target)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
{
|
2023-02-08 22:16:25 +08:00
|
|
|
|
int size = nums.Length;
|
|
|
|
|
// 两层循环,时间复杂度 O(n^2)
|
|
|
|
|
for (int i = 0; i < size - 1; i++)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
{
|
2023-02-08 22:16:25 +08:00
|
|
|
|
for (int j = i + 1; j < size; j++)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
{
|
2023-02-08 22:16:25 +08:00
|
|
|
|
if (nums[i] + nums[j] == target)
|
|
|
|
|
return new int[] { i, j };
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
return new int[0];
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="leetcode_two_sum.swift"
|
2023-02-08 20:30:14 +08:00
|
|
|
|
/* 方法一:暴力枚举 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func twoSumBruteForce(nums: [Int], target: Int) -> [Int] {
|
|
|
|
|
// 两层循环,时间复杂度 O(n^2)
|
|
|
|
|
for i in nums.indices.dropLast() {
|
|
|
|
|
for j in nums.indices.dropFirst(i + 1) {
|
|
|
|
|
if nums[i] + nums[j] == target {
|
|
|
|
|
return [i, j]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return [0]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="leetcode_two_sum.zig"
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 方法一:暴力枚举
|
2023-02-09 23:12:58 +08:00
|
|
|
|
fn twoSumBruteForce(nums: []i32, target: i32) ?[2]i32 {
|
2023-02-09 22:55:29 +08:00
|
|
|
|
var size: usize = nums.len;
|
|
|
|
|
var i: usize = 0;
|
|
|
|
|
// 两层循环,时间复杂度 O(n^2)
|
|
|
|
|
while (i < size - 1) : (i += 1) {
|
|
|
|
|
var j = i + 1;
|
|
|
|
|
while (j < size) : (j += 1) {
|
|
|
|
|
if (nums[i] + nums[j] == target) {
|
|
|
|
|
return [_]i32{@intCast(i32, i), @intCast(i32, j)};
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-09 23:12:58 +08:00
|
|
|
|
return null;
|
2023-02-09 22:55:29 +08:00
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
该方法的时间复杂度为 $O(n^2)$ ,空间复杂度为 $O(1)$ ,**属于以时间换空间**。此方法时间复杂度太高,在大数据量下非常耗时。
|
2023-02-27 03:45:01 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
### 方法二:辅助哈希表
|
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
考虑借助一个哈希表,key-value 分别为数组元素和元素索引。循环遍历数组中的每个元素 num,并执行:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-27 03:45:01 +08:00
|
|
|
|
1. 判断数字 `target - num` 是否在哈希表中,若是则直接返回该两个元素的索引;
|
|
|
|
|
2. 将元素 `num` 和其索引添加进哈希表;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="leetcode_two_sum.java"
|
|
|
|
|
/* 方法二:辅助哈希表 */
|
2023-02-08 16:47:52 +08:00
|
|
|
|
int[] twoSumHashTable(int[] nums, int target) {
|
|
|
|
|
int size = nums.length;
|
|
|
|
|
// 辅助哈希表,空间复杂度 O(n)
|
|
|
|
|
Map<Integer, Integer> dic = new HashMap<>();
|
|
|
|
|
// 单层循环,时间复杂度 O(n)
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
if (dic.containsKey(target - nums[i])) {
|
|
|
|
|
return new int[] { dic.get(target - nums[i]), i };
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
dic.put(nums[i], i);
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
return new int[0];
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="leetcode_two_sum.cpp"
|
|
|
|
|
/* 方法二:辅助哈希表 */
|
2023-02-08 16:47:52 +08:00
|
|
|
|
vector<int> twoSumHashTable(vector<int>& nums, int target) {
|
|
|
|
|
int size = nums.size();
|
|
|
|
|
// 辅助哈希表,空间复杂度 O(n)
|
|
|
|
|
unordered_map<int, int> dic;
|
|
|
|
|
// 单层循环,时间复杂度 O(n)
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
if (dic.find(target - nums[i]) != dic.end()) {
|
|
|
|
|
return { dic[target - nums[i]], i };
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
dic.emplace(nums[i], i);
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
return {};
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="leetcode_two_sum.py"
|
2023-03-23 18:56:56 +08:00
|
|
|
|
def two_sum_hash_table(nums: list[int], target: int) -> list[int]:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""方法二:辅助哈希表"""
|
2023-02-08 16:47:52 +08:00
|
|
|
|
# 辅助哈希表,空间复杂度 O(n)
|
|
|
|
|
dic = {}
|
|
|
|
|
# 单层循环,时间复杂度 O(n)
|
|
|
|
|
for i in range(len(nums)):
|
|
|
|
|
if target - nums[i] in dic:
|
2023-03-12 18:46:03 +08:00
|
|
|
|
return [dic[target - nums[i]], i]
|
2023-02-08 16:47:52 +08:00
|
|
|
|
dic[nums[i]] = i
|
|
|
|
|
return []
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="leetcode_two_sum.go"
|
2023-02-09 04:43:12 +08:00
|
|
|
|
/* 方法二:辅助哈希表 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func twoSumHashTable(nums []int, target int) []int {
|
|
|
|
|
// 辅助哈希表,空间复杂度 O(n)
|
|
|
|
|
hashTable := map[int]int{}
|
|
|
|
|
// 单层循环,时间复杂度 O(n)
|
|
|
|
|
for idx, val := range nums {
|
|
|
|
|
if preIdx, ok := hashTable[target-val]; ok {
|
|
|
|
|
return []int{preIdx, idx}
|
|
|
|
|
}
|
|
|
|
|
hashTable[val] = idx
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="leetcode_two_sum.js"
|
2023-02-08 16:47:52 +08:00
|
|
|
|
/* 方法二:辅助哈希表 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
function twoSumHashTable(nums, target) {
|
|
|
|
|
// 辅助哈希表,空间复杂度 O(n)
|
|
|
|
|
let m = {};
|
|
|
|
|
// 单层循环,时间复杂度 O(n)
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
if (m[nums[i]] !== undefined) {
|
|
|
|
|
return [m[nums[i]], i];
|
|
|
|
|
} else {
|
|
|
|
|
m[target - nums[i]] = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="leetcode_two_sum.ts"
|
2023-02-08 16:47:52 +08:00
|
|
|
|
/* 方法二:辅助哈希表 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
function twoSumHashTable(nums: number[], target: number): number[] {
|
|
|
|
|
// 辅助哈希表,空间复杂度 O(n)
|
|
|
|
|
let m: Map<number, number> = new Map();
|
|
|
|
|
// 单层循环,时间复杂度 O(n)
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
let index = m.get(nums[i]);
|
|
|
|
|
if (index !== undefined) {
|
|
|
|
|
return [index, i];
|
|
|
|
|
} else {
|
|
|
|
|
m.set(target - nums[i], i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="leetcode_two_sum.c"
|
2023-02-11 18:21:44 +08:00
|
|
|
|
[class]{}-[func]{twoSumHashTable}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="leetcode_two_sum.cs"
|
2023-02-08 22:16:25 +08:00
|
|
|
|
/* 方法二:辅助哈希表 */
|
|
|
|
|
int[] twoSumHashTable(int[] nums, int target)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
{
|
2023-02-08 22:16:25 +08:00
|
|
|
|
int size = nums.Length;
|
|
|
|
|
// 辅助哈希表,空间复杂度 O(n)
|
|
|
|
|
Dictionary<int, int> dic = new();
|
|
|
|
|
// 单层循环,时间复杂度 O(n)
|
|
|
|
|
for (int i = 0; i < size; i++)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
{
|
2023-02-08 22:16:25 +08:00
|
|
|
|
if (dic.ContainsKey(target - nums[i]))
|
2023-02-08 15:20:18 +08:00
|
|
|
|
{
|
2023-02-08 22:16:25 +08:00
|
|
|
|
return new int[] { dic[target - nums[i]], i };
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
dic.Add(nums[i], i);
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
return new int[0];
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="leetcode_two_sum.swift"
|
2023-02-08 20:30:14 +08:00
|
|
|
|
/* 方法二:辅助哈希表 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func twoSumHashTable(nums: [Int], target: Int) -> [Int] {
|
|
|
|
|
// 辅助哈希表,空间复杂度 O(n)
|
|
|
|
|
var dic: [Int: Int] = [:]
|
|
|
|
|
// 单层循环,时间复杂度 O(n)
|
|
|
|
|
for i in nums.indices {
|
|
|
|
|
if let j = dic[target - nums[i]] {
|
|
|
|
|
return [j, i]
|
|
|
|
|
}
|
|
|
|
|
dic[nums[i]] = i
|
|
|
|
|
}
|
|
|
|
|
return [0]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="leetcode_two_sum.zig"
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 方法二:辅助哈希表
|
2023-02-09 23:12:58 +08:00
|
|
|
|
fn twoSumHashTable(nums: []i32, target: i32) !?[2]i32 {
|
2023-02-09 22:55:29 +08:00
|
|
|
|
var size: usize = nums.len;
|
|
|
|
|
// 辅助哈希表,空间复杂度 O(n)
|
|
|
|
|
var dic = std.AutoHashMap(i32, i32).init(std.heap.page_allocator);
|
|
|
|
|
defer dic.deinit();
|
|
|
|
|
var i: usize = 0;
|
|
|
|
|
// 单层循环,时间复杂度 O(n)
|
|
|
|
|
while (i < size) : (i += 1) {
|
|
|
|
|
if (dic.contains(target - nums[i])) {
|
|
|
|
|
return [_]i32{dic.get(target - nums[i]).?, @intCast(i32, i)};
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-02-09 22:55:29 +08:00
|
|
|
|
try dic.put(nums[i], @intCast(i32, i));
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-02-09 23:12:58 +08:00
|
|
|
|
return null;
|
2023-02-09 22:55:29 +08:00
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
2023-02-27 03:45:01 +08:00
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
该方法的时间复杂度为 $O(N)$ ,空间复杂度为 $O(N)$ ,**体现了以空间换时间**。尽管此方法引入了额外的空间使用,但在时间和空间的整体效率更为均衡,因此它是本题的最优解法。
|