You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hello-algo/chapter_searching/replace_linear_by_hashing.md

509 lines
15 KiB

2 years ago
---
comments: true
---
1 year ago
# 10.4   哈希优化策略
2 years ago
2 years ago
在算法题中,**我们常通过将线性查找替换为哈希查找来降低算法的时间复杂度**。我们借助一个算法题来加深理解。
2 years ago
2 years ago
!!! question
2 years ago
2 years ago
给定一个整数数组 `nums` 和一个目标元素 `target` ,请在数组中搜索“和”为 `target` 的两个元素,并返回它们的数组索引。返回任意一个解即可。
2 years ago
1 year ago
## 10.4.1   线性查找:以时间换空间
2 years ago
1 year ago
考虑直接遍历所有可能的组合。如图 10-9 所示,我们开启一个两层循环,在每轮中判断两个整数的和是否为 `target` ,若是则返回它们的索引。
2 years ago
2 years ago
![线性查找求解两数之和](replace_linear_by_hashing.assets/two_sum_brute_force.png)
1 year ago
<p align="center"> 图 10-9 &nbsp; 线性查找求解两数之和 </p>
2 years ago
=== "Java"
2 years ago
```java title="two_sum.java"
2 years ago
/* 方法一:暴力枚举 */
2 years ago
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 };
2 years ago
}
}
2 years ago
return new int[0];
2 years ago
}
```
=== "C++"
2 years ago
```cpp title="two_sum.cpp"
2 years ago
/* 方法一:暴力枚举 */
2 years ago
vector<int> twoSumBruteForce(vector<int> &nums, int target) {
2 years ago
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)
2 years ago
return {i, j};
2 years ago
}
}
2 years ago
return {};
}
2 years ago
```
=== "Python"
2 years ago
```python title="two_sum.py"
2 years ago
def two_sum_brute_force(nums: list[int], target: int) -> list[int]:
2 years ago
"""方法一:暴力枚举"""
2 years ago
# 两层循环,时间复杂度 O(n^2)
for i in range(len(nums) - 1):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
2 years ago
return [i, j]
2 years ago
return []
2 years ago
```
=== "Go"
2 years ago
```go title="two_sum.go"
2 years ago
/* 方法一:暴力枚举 */
2 years ago
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
}
```
1 year ago
=== "JS"
2 years ago
2 years ago
```javascript title="two_sum.js"
2 years ago
/* 方法一:暴力枚举 */
2 years ago
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 [];
}
```
1 year ago
=== "TS"
2 years ago
2 years ago
```typescript title="two_sum.ts"
2 years ago
/* 方法一:暴力枚举 */
2 years ago
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 [];
2 years ago
}
2 years ago
```
=== "C"
2 years ago
```c title="two_sum.c"
2 years ago
/* 方法一:暴力枚举 */
int *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {
for (int i = 0; i < numsSize; ++i) {
for (int j = i + 1; j < numsSize; ++j) {
if (nums[i] + nums[j] == target) {
int *res = malloc(sizeof(int) * 2);
res[0] = i, res[1] = j;
*returnSize = 2;
return res;
}
}
}
*returnSize = 0;
return NULL;
}
2 years ago
```
=== "C#"
2 years ago
```csharp title="two_sum.cs"
2 years ago
/* 方法一:暴力枚举 */
2 years ago
int[] twoSumBruteForce(int[] nums, int target) {
2 years ago
int size = nums.Length;
// 两层循环,时间复杂度 O(n^2)
2 years ago
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
2 years ago
if (nums[i] + nums[j] == target)
return new int[] { i, j };
2 years ago
}
}
1 year ago
return Array.Empty<int>();
2 years ago
}
```
=== "Swift"
2 years ago
```swift title="two_sum.swift"
2 years ago
/* 方法一:暴力枚举 */
2 years ago
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"
2 years ago
```zig title="two_sum.zig"
2 years ago
// 方法一:暴力枚举
2 years ago
fn twoSumBruteForce(nums: []i32, target: i32) ?[2]i32 {
2 years ago
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) {
1 year ago
return [_]i32{@intCast(i), @intCast(j)};
2 years ago
}
}
}
2 years ago
return null;
2 years ago
}
2 years ago
```
1 year ago
=== "Dart"
```dart title="two_sum.dart"
/* 方法一: 暴力枚举 */
List<int> twoSumBruteForce(List<int> nums, int target) {
int size = nums.length;
1 year ago
// 两层循环,时间复杂度 O(n^2)
1 year ago
for (var i = 0; i < size - 1; i++) {
for (var j = i + 1; j < size; j++) {
if (nums[i] + nums[j] == target) return [i, j];
}
}
return [0];
}
```
1 year ago
=== "Rust"
```rust title="two_sum.rs"
/* 方法一:暴力枚举 */
pub fn two_sum_brute_force(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {
let size = nums.len();
// 两层循环,时间复杂度 O(n^2)
for i in 0..size - 1 {
for j in i + 1..size {
if nums[i] + nums[j] == target {
return Some(vec![i as i32, j as i32]);
}
}
}
None
}
```
2 years ago
此方法的时间复杂度为 $O(n^2)$ ,空间复杂度为 $O(1)$ ,在大数据量下非常耗时。
2 years ago
1 year ago
## 10.4.2 &nbsp; 哈希查找:以空间换时间
2 years ago
1 year ago
考虑借助一个哈希表,键值对分别为数组元素和元素索引。循环遍历数组,每轮执行图 10-10 所示的步骤。
2 years ago
1 year ago
1. 判断数字 `target - nums[i]` 是否在哈希表中,若是则直接返回这两个元素的索引。
2. 将键值对 `nums[i]` 和索引 `i` 添加进哈希表。
2 years ago
=== "<1>"
2 years ago
![辅助哈希表求解两数之和](replace_linear_by_hashing.assets/two_sum_hashtable_step1.png)
2 years ago
=== "<2>"
![two_sum_hashtable_step2](replace_linear_by_hashing.assets/two_sum_hashtable_step2.png)
2 years ago
2 years ago
=== "<3>"
![two_sum_hashtable_step3](replace_linear_by_hashing.assets/two_sum_hashtable_step3.png)
2 years ago
1 year ago
<p align="center"> 图 10-10 &nbsp; 辅助哈希表求解两数之和 </p>
1 year ago
2 years ago
实现代码如下所示,仅需单层循环即可。
2 years ago
2 years ago
=== "Java"
2 years ago
```java title="two_sum.java"
2 years ago
/* 方法二:辅助哈希表 */
2 years ago
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 };
2 years ago
}
2 years ago
dic.put(nums[i], i);
2 years ago
}
2 years ago
return new int[0];
2 years ago
}
```
=== "C++"
2 years ago
```cpp title="two_sum.cpp"
2 years ago
/* 方法二:辅助哈希表 */
2 years ago
vector<int> twoSumHashTable(vector<int> &nums, int target) {
2 years ago
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()) {
2 years ago
return {dic[target - nums[i]], i};
2 years ago
}
2 years ago
dic.emplace(nums[i], i);
2 years ago
}
2 years ago
return {};
}
2 years ago
```
=== "Python"
2 years ago
```python title="two_sum.py"
2 years ago
def two_sum_hash_table(nums: list[int], target: int) -> list[int]:
2 years ago
"""方法二:辅助哈希表"""
2 years ago
# 辅助哈希表,空间复杂度 O(n)
dic = {}
# 单层循环,时间复杂度 O(n)
for i in range(len(nums)):
if target - nums[i] in dic:
2 years ago
return [dic[target - nums[i]], i]
2 years ago
dic[nums[i]] = i
return []
2 years ago
```
=== "Go"
2 years ago
```go title="two_sum.go"
2 years ago
/* 方法二:辅助哈希表 */
2 years ago
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
}
```
1 year ago
=== "JS"
2 years ago
2 years ago
```javascript title="two_sum.js"
2 years ago
/* 方法二:辅助哈希表 */
2 years ago
function twoSumHashTable(nums, target) {
// 辅助哈希表,空间复杂度 O(n)
let m = {};
// 单层循环,时间复杂度 O(n)
for (let i = 0; i < nums.length; i++) {
1 year ago
if (m[target - nums[i]] !== undefined) {
1 year ago
return [m[target - nums[i]], i];
2 years ago
} else {
1 year ago
m[nums[i]] = i;
2 years ago
}
}
return [];
}
```
1 year ago
=== "TS"
2 years ago
2 years ago
```typescript title="two_sum.ts"
2 years ago
/* 方法二:辅助哈希表 */
2 years ago
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++) {
1 year ago
let index = m.get(target - nums[i]);
2 years ago
if (index !== undefined) {
return [index, i];
} else {
1 year ago
m.set(nums[i], i);
2 years ago
}
}
return [];
2 years ago
}
2 years ago
```
=== "C"
2 years ago
```c title="two_sum.c"
2 years ago
/* 哈希表 */
struct hashTable {
int key;
int val;
UT_hash_handle hh; // 基于 uthash.h 实现
};
2 years ago
typedef struct hashTable hashTable;
2 years ago
/* 哈希表查询 */
hashTable *find(hashTable *h, int key) {
hashTable *tmp;
HASH_FIND_INT(h, &key, tmp);
return tmp;
}
/* 哈希表元素插入 */
void insert(hashTable *h, int key, int val) {
hashTable *t = find(h, key);
if (t == NULL) {
hashTable *tmp = malloc(sizeof(hashTable));
tmp->key = key, tmp->val = val;
HASH_ADD_INT(h, key, tmp);
} else {
t->val = val;
}
}
2 years ago
/* 方法二:辅助哈希表 */
int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {
hashTable *hashtable = NULL;
for (int i = 0; i < numsSize; i++) {
hashTable *t = find(hashtable, target - nums[i]);
if (t != NULL) {
int *res = malloc(sizeof(int) * 2);
res[0] = t->val, res[1] = i;
*returnSize = 2;
return res;
}
insert(hashtable, nums[i], i);
}
*returnSize = 0;
return NULL;
}
2 years ago
```
=== "C#"
2 years ago
```csharp title="two_sum.cs"
2 years ago
/* 方法二:辅助哈希表 */
2 years ago
int[] twoSumHashTable(int[] nums, int target) {
2 years ago
int size = nums.Length;
// 辅助哈希表,空间复杂度 O(n)
Dictionary<int, int> dic = new();
// 单层循环,时间复杂度 O(n)
2 years ago
for (int i = 0; i < size; i++) {
if (dic.ContainsKey(target - nums[i])) {
2 years ago
return new int[] { dic[target - nums[i]], i };
2 years ago
}
2 years ago
dic.Add(nums[i], i);
2 years ago
}
1 year ago
return Array.Empty<int>();
2 years ago
}
```
=== "Swift"
2 years ago
```swift title="two_sum.swift"
2 years ago
/* 方法二:辅助哈希表 */
2 years ago
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"
2 years ago
```zig title="two_sum.zig"
2 years ago
// 方法二:辅助哈希表
2 years ago
fn twoSumHashTable(nums: []i32, target: i32) !?[2]i32 {
2 years ago
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])) {
1 year ago
return [_]i32{dic.get(target - nums[i]).?, @intCast(i)};
2 years ago
}
1 year ago
try dic.put(nums[i], @intCast(i));
2 years ago
}
2 years ago
return null;
2 years ago
}
2 years ago
```
2 years ago
1 year ago
=== "Dart"
```dart title="two_sum.dart"
/* 方法二: 辅助哈希表 */
List<int> twoSumHashTable(List<int> nums, int target) {
int size = nums.length;
1 year ago
// 辅助哈希表,空间复杂度 O(n)
1 year ago
Map<int, int> dic = HashMap();
1 year ago
// 单层循环,时间复杂度 O(n)
1 year ago
for (var i = 0; i < size; i++) {
if (dic.containsKey(target - nums[i])) {
return [dic[target - nums[i]]!, i];
}
dic.putIfAbsent(nums[i], () => i);
}
return [0];
}
```
1 year ago
=== "Rust"
```rust title="two_sum.rs"
/* 方法二:辅助哈希表 */
pub fn two_sum_hash_table(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {
// 辅助哈希表,空间复杂度 O(n)
let mut dic = HashMap::new();
// 单层循环,时间复杂度 O(n)
for (i, num) in nums.iter().enumerate() {
match dic.get(&(target - num)) {
Some(v) => return Some(vec![*v as i32, i as i32]),
None => dic.insert(num, i as i32)
};
}
None
}
```
2 years ago
此方法通过哈希查找将时间复杂度从 $O(n^2)$ 降低至 $O(n)$ ,大幅提升运行效率。
由于需要维护一个额外的哈希表,因此空间复杂度为 $O(n)$ 。**尽管如此,该方法的整体时空效率更为均衡,因此它是本题的最优解法**。