2023-04-17 18:22:18 +08:00
|
|
|
# 哈希优化策略
|
2022-11-22 17:47:26 +08:00
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
在算法题中,**我们常通过将线性查找替换为哈希查找来降低算法的时间复杂度**。我们借助一个算法题来加深理解。
|
2023-01-18 19:56:56 +08:00
|
|
|
|
2023-05-21 19:58:21 +08:00
|
|
|
!!! question
|
2023-01-18 19:56:56 +08:00
|
|
|
|
2023-05-21 19:58:21 +08:00
|
|
|
给定一个整数数组 `nums` 和一个目标元素 `target` ,请在数组中搜索“和”为 `target` 的两个元素,并返回它们的数组索引。返回任意一个解即可。
|
2023-01-18 19:56:56 +08:00
|
|
|
|
2023-04-17 18:22:18 +08:00
|
|
|
## 线性查找:以时间换空间
|
2022-11-22 17:47:26 +08:00
|
|
|
|
2023-08-21 19:33:45 +08:00
|
|
|
考虑直接遍历所有可能的组合。如下图所示,我们开启一个两层循环,在每轮中判断两个整数的和是否为 `target` ,若是则返回它们的索引。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
![线性查找求解两数之和](replace_linear_by_hashing.assets/two_sum_brute_force.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```java title="two_sum.java"
|
|
|
|
[class]{two_sum}-[func]{twoSumBruteForce}
|
2022-11-22 17:47:26 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```cpp title="two_sum.cpp"
|
2023-02-08 19:45:06 +08:00
|
|
|
[class]{}-[func]{twoSumBruteForce}
|
2022-11-22 17:47:26 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```python title="two_sum.py"
|
2023-02-08 19:45:06 +08:00
|
|
|
[class]{}-[func]{two_sum_brute_force}
|
2022-11-22 17:47:26 +08:00
|
|
|
```
|
|
|
|
|
2022-11-24 22:54:10 +08:00
|
|
|
=== "Go"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```go title="two_sum.go"
|
2023-02-09 04:45:06 +08:00
|
|
|
[class]{}-[func]{twoSumBruteForce}
|
2022-11-24 22:54:10 +08:00
|
|
|
```
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
=== "JS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```javascript title="two_sum.js"
|
2023-02-08 19:45:06 +08:00
|
|
|
[class]{}-[func]{twoSumBruteForce}
|
2022-12-03 01:31:29 +08:00
|
|
|
```
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
=== "TS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```typescript title="two_sum.ts"
|
2023-02-08 19:45:06 +08:00
|
|
|
[class]{}-[func]{twoSumBruteForce}
|
2022-12-03 01:31:29 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```c title="two_sum.c"
|
2023-02-11 18:22:27 +08:00
|
|
|
[class]{}-[func]{twoSumBruteForce}
|
2022-12-03 01:31:29 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```csharp title="two_sum.cs"
|
|
|
|
[class]{two_sum}-[func]{twoSumBruteForce}
|
2022-12-03 01:31:29 +08:00
|
|
|
```
|
|
|
|
|
2023-01-03 22:24:50 +08:00
|
|
|
=== "Swift"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```swift title="two_sum.swift"
|
2023-02-08 20:30:05 +08:00
|
|
|
[class]{}-[func]{twoSumBruteForce}
|
2023-01-03 22:24:50 +08:00
|
|
|
```
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
=== "Zig"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```zig title="two_sum.zig"
|
2023-02-09 22:57:25 +08:00
|
|
|
[class]{}-[func]{twoSumBruteForce}
|
2023-02-01 22:03:04 +08:00
|
|
|
```
|
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
```dart title="two_sum.dart"
|
|
|
|
[class]{}-[func]{twoSumBruteForce}
|
|
|
|
```
|
|
|
|
|
2023-07-26 11:00:53 +08:00
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
```rust title="two_sum.rs"
|
|
|
|
[class]{}-[func]{two_sum_brute_force}
|
|
|
|
```
|
|
|
|
|
2023-04-17 18:22:18 +08:00
|
|
|
此方法的时间复杂度为 $O(n^2)$ ,空间复杂度为 $O(1)$ ,在大数据量下非常耗时。
|
2023-02-27 02:44:09 +08:00
|
|
|
|
2023-04-17 18:22:18 +08:00
|
|
|
## 哈希查找:以空间换时间
|
2022-11-22 17:47:26 +08:00
|
|
|
|
2023-08-21 19:33:45 +08:00
|
|
|
考虑借助一个哈希表,键值对分别为数组元素和元素索引。循环遍历数组,每轮执行下图所示的步骤。
|
2023-05-09 00:36:18 +08:00
|
|
|
|
2023-07-26 08:59:36 +08:00
|
|
|
1. 判断数字 `target - nums[i]` 是否在哈希表中,若是则直接返回这两个元素的索引。
|
|
|
|
2. 将键值对 `nums[i]` 和索引 `i` 添加进哈希表。
|
2023-05-09 00:36:18 +08:00
|
|
|
|
|
|
|
=== "<1>"
|
2023-05-10 21:00:04 +08:00
|
|
|
![辅助哈希表求解两数之和](replace_linear_by_hashing.assets/two_sum_hashtable_step1.png)
|
2023-05-09 00:36:18 +08:00
|
|
|
|
|
|
|
=== "<2>"
|
|
|
|
![two_sum_hashtable_step2](replace_linear_by_hashing.assets/two_sum_hashtable_step2.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
=== "<3>"
|
|
|
|
![two_sum_hashtable_step3](replace_linear_by_hashing.assets/two_sum_hashtable_step3.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
实现代码如下所示,仅需单层循环即可。
|
2023-04-17 18:22:18 +08:00
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
=== "Java"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```java title="two_sum.java"
|
|
|
|
[class]{two_sum}-[func]{twoSumHashTable}
|
2022-11-22 17:47:26 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```cpp title="two_sum.cpp"
|
2023-02-08 19:45:06 +08:00
|
|
|
[class]{}-[func]{twoSumHashTable}
|
2022-11-22 17:47:26 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```python title="two_sum.py"
|
2023-02-08 19:45:06 +08:00
|
|
|
[class]{}-[func]{two_sum_hash_table}
|
2022-11-22 17:47:26 +08:00
|
|
|
```
|
2022-11-24 22:54:10 +08:00
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```go title="two_sum.go"
|
2023-02-09 04:45:06 +08:00
|
|
|
[class]{}-[func]{twoSumHashTable}
|
2022-11-24 22:54:10 +08:00
|
|
|
```
|
2022-12-03 01:31:29 +08:00
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
=== "JS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```javascript title="two_sum.js"
|
2023-02-08 19:45:06 +08:00
|
|
|
[class]{}-[func]{twoSumHashTable}
|
2022-12-03 01:31:29 +08:00
|
|
|
```
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
=== "TS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```typescript title="two_sum.ts"
|
2023-02-08 19:45:06 +08:00
|
|
|
[class]{}-[func]{twoSumHashTable}
|
2022-12-03 01:31:29 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```c title="two_sum.c"
|
2023-04-18 21:19:57 +08:00
|
|
|
[class]{hashTable}-[func]{}
|
|
|
|
|
2023-02-11 18:22:27 +08:00
|
|
|
[class]{}-[func]{twoSumHashTable}
|
2022-12-03 01:31:29 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```csharp title="two_sum.cs"
|
|
|
|
[class]{two_sum}-[func]{twoSumHashTable}
|
2022-12-03 01:31:29 +08:00
|
|
|
```
|
2023-01-03 22:24:50 +08:00
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```swift title="two_sum.swift"
|
2023-02-08 20:30:05 +08:00
|
|
|
[class]{}-[func]{twoSumHashTable}
|
2023-01-03 22:24:50 +08:00
|
|
|
```
|
2023-02-01 22:03:04 +08:00
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
2023-05-09 00:36:18 +08:00
|
|
|
```zig title="two_sum.zig"
|
2023-02-09 22:57:25 +08:00
|
|
|
[class]{}-[func]{twoSumHashTable}
|
2023-02-01 22:03:04 +08:00
|
|
|
```
|
2023-02-27 02:44:09 +08:00
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
```dart title="two_sum.dart"
|
|
|
|
[class]{}-[func]{twoSumHashTable}
|
|
|
|
```
|
|
|
|
|
2023-07-26 11:00:53 +08:00
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
```rust title="two_sum.rs"
|
|
|
|
[class]{}-[func]{two_sum_hash_table}
|
|
|
|
```
|
|
|
|
|
2023-04-17 18:22:18 +08:00
|
|
|
此方法通过哈希查找将时间复杂度从 $O(n^2)$ 降低至 $O(n)$ ,大幅提升运行效率。
|
|
|
|
|
|
|
|
由于需要维护一个额外的哈希表,因此空间复杂度为 $O(n)$ 。**尽管如此,该方法的整体时空效率更为均衡,因此它是本题的最优解法**。
|