Skip to content

Commit dcce58a

Browse files
authored
feat(translation): $1.TwoSum (azl397985856#358)
1 parent 032d353 commit dcce58a

File tree

2 files changed

+65
-13
lines changed

2 files changed

+65
-13
lines changed

README.en.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ If you want to do some contributions or collaborations, just feel free to contac
5252

5353
- For the parts that were added recently, there will be a 🆕 behind.
5454
- 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.
5556
- Here will be the place to update Anki Flashcards in the future as well.
5657
- 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.
5758

@@ -76,13 +77,13 @@ The data structures mainly include:
7677
- Tree and Graph: Lowest Common Ancestor (LCA); Disjoint-Set
7778
- String: Prefix Tree (Trie); Suffix Tree
7879

79-
## Previews(Not Translated Yet)
80+
## Previews (Translation in Progress)
8081

8182
[0042.trapping-rain-water](./problems/42.trapping-rain-water.md):
8283

8384
![0042.trapping-rain-water](./assets/problems/42.trapping-rain-water-1.png)
8485

85-
[0547.friend-circles](./problems/547.friend-circles-en.md):
86+
[0547.friend-circles](./problems/547.friend-circles-en.md):
8687

8788
![friend circle BFS](./assets/problems/547.friend-circle-bfs.png)
8889

@@ -110,13 +111,13 @@ The data structures mainly include:
110111

111112
> Here only lists some **representative problems** but not all.
112113
113-
#### Easy (Not Translated Yet)
114114

115-
- [0001.TwoSum](./problems/1.TwoSum.md)🆕
115+
#### Easy (Translation in Progress)
116+
- [0001.TwoSum](./problems/1.TwoSum.en.md)🆕✅
116117
- [0020.Valid Parentheses](./problems/20.validParentheses.md)
117118
- [0021.MergeTwoSortedLists](./problems/21.MergeTwoSortedLists.md) 🆕
118119
- [0026.remove-duplicates-from-sorted-array](./problems/26.remove-duplicates-from-sorted-array.md)
119-
- [0053.maximum-sum-subarray](./problems/53.maximum-sum-subarray-en.md) 🆕
120+
- [0053.maximum-sum-subarray](./problems/53.maximum-sum-subarray-en.md) 🆕
120121
- [0088.merge-sorted-array](./problems/88.merge-sorted-array.md)
121122
- [0104.maximum-depth-of-binary-tree](./problems/104.maximum-depth-of-binary-tree.md)
122123
- [0121.best-time-to-buy-and-sell-stock](./problems/121.best-time-to-buy-and-sell-stock.md)
@@ -145,7 +146,7 @@ The data structures mainly include:
145146
- [0501.find-mode-in-binary-search-tree](./problems/501.Find-Mode-in-Binary-Search-Tree.md) 🆕
146147
- [0575.distribute-candies](./problems/575.distribute-candies.md)
147148

148-
#### Medium (Not Translated Yet)
149+
#### Medium (Translation in Progress)
149150

150151
- [0002. Add Two Numbers](./problems/2.addTwoNumbers.md)
151152
- [0003. Longest Substring Without Repeating Characters](./problems/3.longestSubstringWithoutRepeatingCharacters.md)
@@ -171,7 +172,7 @@ The data structures mainly include:
171172
- [0073.set-matrix-zeroes](./problems/73.set-matrix-zeroes.md)
172173
- [0075.sort-colors](./problems/75.sort-colors.md)
173174
- [0078.subsets](./problems/78.subsets.md)
174-
- [0079.word-search](./problems/79.word-search-en.md)
175+
- [0079.word-search](./problems/79.word-search-en.md)
175176
- [0086.partition-list](./problems/86.partition-list.md)
176177
- [0090.subsets-ii](./problems/90.subsets-ii.md)
177178
- [0091.decode-ways](./problems/91.decode-ways.md)
@@ -211,11 +212,11 @@ The data structures mainly include:
211212
- [0416.partition-equal-subset-sum](./problems/416.partition-equal-subset-sum.md)
212213
- [0445.add-two-numbers-ii](./problems/445.add-two-numbers-ii.md)
213214
- [0454.4-sum-ii](./problems/454.4-sum-ii.md)
214-
- [0474.ones-and-zeros](./problems/474.ones-and-zeros-en.md)
215+
- [0474.ones-and-zeros](./problems/474.ones-and-zeros-en.md)
215216
- [0494.target-sum](./problems/494.target-sum.md)
216217
- [0516.longest-palindromic-subsequence](./problems/516.longest-palindromic-subsequence.md)
217218
- [0518.coin-change-2](./problems/518.coin-change-2.md)
218-
- [0547.friend-circles](./problems/547.friend-circles-en.md) 🆕
219+
- [0547.friend-circles](./problems/547.friend-circles-en.md) 🆕
219220
- [0609.find-duplicate-file-in-system](./problems/609.find-duplicate-file-in-system.md)
220221
- [0875.koko-eating-bananas](./problems/875.koko-eating-bananas.md)
221222
- [0877.stone-game](./problems/877.stone-game.md)
@@ -225,11 +226,12 @@ The data structures mainly include:
225226
- [1031.maximum-sum-of-two-non-overlapping-subarrays](./problems/1031.maximum-sum-of-two-non-overlapping-subarrays.md)
226227
- [1218.longest-arithmetic-subsequence-of-given-difference.md](./problems/1218.longest-arithmetic-subsequence-of-given-difference.md) 🆕
227228

228-
#### Hard (Not Translated Yet)
229+
230+
#### Hard (Translation in Progress)
229231

230232
- [0004.median-of-two-sorted-array](./problems/4.median-of-two-sorted-array.md) 🆕
231233
- [0023.merge-k-sorted-lists](./problems/23.merge-k-sorted-lists.md)
232-
- [0025.reverse-nodes-in-k-group](./problems/25.reverse-nodes-in-k-groups-en.md) 🆕
234+
- [0025.reverse-nodes-in-k-group](./problems/25.reverse-nodes-in-k-groups-en.md) 🆕
233235
- [0032.longest-valid-parentheses](./problems/32.longest-valid-parentheses.md) 🆕
234236
- [0042.trapping-rain-water](./problems/42.trapping-rain-water.md)
235237
- [0052.N-Queens-II](./problems/52.N-Queens-II.md) 🆕
@@ -240,12 +242,12 @@ The data structures mainly include:
240242
- [0295.find-median-from-data-stream](./problems/295.find-median-from-data-stream.md) 🆕
241243
- [0301.remove-invalid-parentheses](./problems/301.remove-invalid-parentheses.md)
242244
- [0460.lfu-cache](./problems/460.lfu-cache.md) 🆕
243-
- [1168.optimize-water-distribution-in-a-village](./problems/1168.optimize-water-distribution-in-a-village-en.md) 🆕
245+
- [1168.optimize-water-distribution-in-a-village](./problems/1168.optimize-water-distribution-in-a-village-en.md) 🆕
244246

245247
### Summary of Data Structures and Algorithm
246248

247249
- [Data Structure](./thinkings/basic-data-structure-en.md) (Drafts)
248-
- [Basic Algorithm](./thinkings/basic-algorithm-en.md)Drafts
250+
- [Basic Algorithm](./thinkings/basic-algorithm-en.md)(Drafts)
249251
- [Binary Tree Traversal](./thinkings/binary-tree-traversal-en.md)
250252
- [Dynamic Programming](./thinkings/dynamic-programming-en.md)
251253
- [Huffman Encode and Run Length Encode](./thinkings/run-length-encode-and-huffman-encode-en.md)

problems/1.TwoSum.en.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Problem
2+
https://leetcode-cn.com/problems/two-sum
3+
4+
## Problem Description
5+
```
6+
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)
25+
26+
## Code
27+
- Support Language: JS
28+
29+
```js
30+
/**
31+
* @param {number[]} nums
32+
* @param {number} target
33+
* @return {number[]}
34+
*/
35+
const twoSum = function (nums, target) {
36+
const map = new Map();
37+
for (let i = 0; i < nums.length; i++) {
38+
const diff = target - nums[i];
39+
if (map.has(diff)) {
40+
return [map.get(diff), i];
41+
}
42+
map.set(nums[i], i);
43+
}
44+
}
45+
```
46+
47+
***Complexity Anlysis***
48+
49+
- *Time Complexity*: O(N)
50+
- *Space Complexity*:O(N)

0 commit comments

Comments
 (0)