You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.en.md
+15-13Lines changed: 15 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -52,6 +52,7 @@ If you want to do some contributions or collaborations, just feel free to contac
52
52
53
53
- For the parts that were added recently, there will be a 🆕 behind.
54
54
- For the parts that were updated recently, there will be a 🖊 behind.
55
+
- For the parts that have been translated, there will be a ✅ behind.
55
56
- Here will be the place to update Anki Flashcards in the future as well.
56
57
- Here is a mind mapping graph showing the summary of categorizations of problems that are questioned frequently in interviews. We could analyze according to the information in the graph.
57
58
@@ -76,13 +77,13 @@ The data structures mainly include:
76
77
- Tree and Graph: Lowest Common Ancestor (LCA); Disjoint-Set
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
7
+
8
+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
9
+
10
+
Example:
11
+
12
+
Given nums = [2, 7, 11, 15], target = 9,
13
+
14
+
Because nums[0] + nums[1] = 2 + 7 = 9,
15
+
return [0, 1].
16
+
```
17
+
18
+
## Solution
19
+
The easiest solution to come up with is Brute Force. We could write two for-loops to traverse every element, and find the target numbers that meet the requirement. However, the time complexity of this solution is O(N^2), while the space complexity is O(1). Apparently, we need to find a way to optimize this solution since the time complexity is too high. What we could do is to record the numbers we have traversed and the relevant index with a Map. Whenever we meet a new number during traversal, we go back to the Map and check whether the `diff` between this number and the target number appeared before. If it did, the problem has been solved and there's no need to continue.
20
+
21
+
## Key Points
22
+
- Find the difference instead of the sum
23
+
- Connect every number with its index through the help of Map
24
+
- Less time by more space. Reduce the time complexity from O(N) to O(1)
0 commit comments