mirror of
https://github.com/krahets/hello-algo.git
synced 2026-09-04 22:27:16 +00:00
build
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 10.3. 哈希优化策略
|
||||
# 10.4. 哈希优化策略
|
||||
|
||||
在算法题中,**我们常通过将线性查找替换为哈希查找来降低算法的时间复杂度**。我们借助一个算法题来加深理解。
|
||||
|
||||
@@ -10,7 +10,7 @@ comments: true
|
||||
|
||||
给定一个整数数组 `nums` 和一个目标元素 `target` ,请在数组中搜索“和”为 `target` 的两个元素,并返回它们的数组索引。返回任意一个解即可。
|
||||
|
||||
## 10.3.1. 线性查找:以时间换空间
|
||||
## 10.4.1. 线性查找:以时间换空间
|
||||
|
||||
考虑直接遍历所有可能的组合。开启一个两层循环,在每轮中判断两个整数的和是否为 `target` ,若是,则返回它们的索引。
|
||||
|
||||
@@ -228,7 +228,7 @@ comments: true
|
||||
|
||||
此方法的时间复杂度为 $O(n^2)$ ,空间复杂度为 $O(1)$ ,在大数据量下非常耗时。
|
||||
|
||||
## 10.3.2. 哈希查找:以空间换时间
|
||||
## 10.4.2. 哈希查找:以空间换时间
|
||||
|
||||
考虑借助一个哈希表,键值对分别为数组元素和元素索引。循环遍历数组,每轮执行:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user