Skip to content

Commit b6939da

Browse files
K3v123krahets
andauthored
translation: Update replace_linear_by_hashing.md (#1551)
* translation: Update replace_linear_by_hashing.md refined some parts of it. * Update replace_linear_by_hashing.md * Update replace_linear_by_hashing.md --------- Co-authored-by: Yudong Jin <[email protected]>
1 parent abf1f11 commit b6939da

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

en/docs/chapter_searching/replace_linear_by_hashing.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Hash optimization strategies
22

3-
In algorithm problems, **we often reduce the time complexity of algorithms by replacing linear search with hash search**. Let's use an algorithm problem to deepen understanding.
3+
In algorithm problems, **we often reduce the time complexity of an algorithm by replacing a linear search with a hash-based search**. Let's use an algorithm problem to deepen the understanding.
44

55
!!! question
66

77
Given an integer array `nums` and a target element `target`, please search for two elements in the array whose "sum" equals `target`, and return their array indices. Any solution is acceptable.
88

99
## Linear search: trading time for space
1010

11-
Consider traversing all possible combinations directly. As shown in the figure below, we initiate a two-layer loop, and in each round, we determine whether the sum of the two integers equals `target`. If so, we return their indices.
11+
Consider traversing through all possible combinations directly. As shown in the figure below, we initiate a nested loop, and in each iteration, we determine whether the sum of the two integers equals `target`. If so, we return their indices.
1212

1313
![Linear search solution for two-sum problem](replace_linear_by_hashing.assets/two_sum_brute_force.png)
1414

@@ -18,11 +18,11 @@ The code is shown below:
1818
[file]{two_sum}-[class]{}-[func]{two_sum_brute_force}
1919
```
2020

21-
This method has a time complexity of $O(n^2)$ and a space complexity of $O(1)$, which is very time-consuming with large data volumes.
21+
This method has a time complexity of $O(n^2)$ and a space complexity of $O(1)$, which can be very time-consuming with large data volumes.
2222

2323
## Hash search: trading space for time
2424

25-
Consider using a hash table, with key-value pairs being the array elements and their indices, respectively. Loop through the array, performing the steps shown in the figure below each round.
25+
Consider using a hash table, where the key-value pairs are the array elements and their indices, respectively. Loop through the array, performing the steps shown in the figure below during each iteration.
2626

2727
1. Check if the number `target - nums[i]` is in the hash table. If so, directly return the indices of these two elements.
2828
2. Add the key-value pair `nums[i]` and index `i` to the hash table.
@@ -42,6 +42,6 @@ The implementation code is shown below, requiring only a single loop:
4242
[file]{two_sum}-[class]{}-[func]{two_sum_hash_table}
4343
```
4444

45-
This method reduces the time complexity from $O(n^2)$ to $O(n)$ by using hash search, greatly improving the running efficiency.
45+
This method reduces the time complexity from $O(n^2)$ to $O(n)$ by using hash search, significantly enhancing runtime efficiency.
4646

4747
As it requires maintaining an additional hash table, the space complexity is $O(n)$. **Nevertheless, this method has a more balanced time-space efficiency overall, making it the optimal solution for this problem**.

0 commit comments

Comments
 (0)