Skip to content

Commit 8e135c1

Browse files
author
lucifer
committed
fix: 文件名重命名
1 parent c763807 commit 8e135c1

9 files changed

+27
-22
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ leetcode 题解,记录自己的 leetcode 解题之路。
142142
143143
#### 简单难度
144144

145-
- [0001.TwoSum](./problems/1.TwoSum.md)
146-
- [0020.Valid Parentheses](./problems/20.validParentheses.md)
147-
- [0021.MergeTwoSortedLists](./problems/21.MergeTwoSortedLists.md)
145+
- [0001.two-sum](./problems/1.two-sum.md)
146+
- [0020.Valid Parentheses](./problems/20.valid-parentheses.md)
147+
- [0021.MergeTwoSortedLists](./problems/21.merge-two-sorted-lists.md)
148148
- [0026.remove-duplicates-from-sorted-array](./problems/26.remove-duplicates-from-sorted-array.md)
149149
- [0053.maximum-sum-subarray](./problems/53.maximum-sum-subarray-cn.md)
150150
- [0088.merge-sorted-array](./problems/88.merge-sorted-array.md)
@@ -181,14 +181,14 @@ leetcode 题解,记录自己的 leetcode 解题之路。
181181

182182
#### 中等难度
183183

184-
- [0002. Add Two Numbers](./problems/2.addTwoNumbers.md)
185-
- [0003. Longest Substring Without Repeating Characters](./problems/3.longestSubstringWithoutRepeatingCharacters.md)
184+
- [0002.add-two-numbers](./problems/2.add-two-numbers.md)
185+
- [0003.longest-substring-without-repeating-characters](./problems/3.longest-substring-without-repeating-characters.md)
186186
- [0005.longest-palindromic-substring](./problems/5.longest-palindromic-substring.md)
187187
- [0011.container-with-most-water](./problems/11.container-with-most-water.md)
188188
- [0015.3-sum](./problems/15.3-sum.md)
189189
- [0017.Letter-Combinations-of-a-Phone-Number](./problems/17.Letter-Combinations-of-a-Phone-Number.md) 🆕
190190
- [0019. Remove Nth Node From End of List](./problems/19.removeNthNodeFromEndofList.md)
191-
- [0022.GenerateParentheses](./problems/22.GenerateParentheses.md) 🆕
191+
- [0022.generate-parentheses.md](./problems/22.generate-parentheses.md) 🆕
192192
- [0024. Swap Nodes In Pairs](./problems/24.swapNodesInPairs.md)
193193
- [0029.divide-two-integers](./problems/29.divide-two-integers.md)
194194
- [0031.next-permutation](./problems/31.next-permutation.md)
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
## Problem
2+
23
https://leetcode-cn.com/problems/two-sum
34

45
## Problem Description
6+
57
```
68
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
79
@@ -16,15 +18,18 @@ return [0, 1].
1618
```
1719

1820
## Solution
21+
1922
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.
2023

2124
## 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)
2525

26-
## Code
27-
- Support Language: JS
26+
- Find the difference instead of the sum
27+
- Connect every number with its index through the help of Map
28+
- Less time by more space. Reduce the time complexity from O(N) to O(1)
29+
30+
## Code
31+
32+
- Support Language: JS
2833

2934
```js
3035
/**
@@ -33,18 +38,18 @@ The easiest solution to come up with is Brute Force. We could write two for-loop
3338
* @return {number[]}
3439
*/
3540
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);
41+
const map = new Map();
42+
for (let i = 0; i < nums.length; i++) {
43+
const diff = target - nums[i];
44+
if (map.has(diff)) {
45+
return [map.get(diff), i];
4346
}
44-
}
47+
map.set(nums[i], i);
48+
}
49+
};
4550
```
4651

47-
***Complexity Anlysis***
52+
**_Complexity Anlysis_**
4853

49-
- *Time Complexity*: O(N)
50-
- *Space Complexity*:O(N)
54+
- _Time Complexity_: O(N)
55+
- _Space Complexity_:O(N)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)