Skip to content

Commit 2cdd9b1

Browse files
committed
Updated some markdown files.
1 parent ebbf936 commit 2cdd9b1

25 files changed

+337
-314
lines changed

en/1-1000/1-two-sum.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
LeetCode link: [1. Two Sum](https://leetcode.com/problems/two-sum), difficulty: **Easy**
33

44
## LeetCode description of "1. Two Sum"
5-
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
5+
Given an array of integers `nums` and an integer `target`, return *indices of the two numbers such that they add up to `target`*.
66

7-
You may assume that each input would have **_exactly_ one solution**, and you may not use the same element twice.
7+
You may assume that each input would have ***exactly* one solution**, and you may not use the same element twice.
88

99
You can return the answer in any order.
1010

@@ -21,9 +21,9 @@ You can return the answer in any order.
2121
**Output**: `[1,2]`
2222

2323
### [Constraints]
24-
- `2 <= nums.length <= 10**4`
25-
- `-10**9 <= nums[i] <= 10**9`
26-
- `-10**9 <= target <= 10**9`
24+
- `2 <= nums.length <= 10^4`
25+
- `-10^9 <= nums[i] <= 10^9`
26+
- `-10^9 <= target <= 10^9`
2727
- **Only one valid answer exists.**
2828

2929
### [Hints]
@@ -56,26 +56,28 @@ The second train of thought is, without changing the array, can we use additiona
5656

5757
### Steps
5858
1. In `Map`, `key` is `num`, and `value` is array `index`.
59-
```javascript
60-
let numToIndex = new Map()
6159

62-
for (let i = 0; i < nums.length; i++) {
63-
numToIndex.set(nums[i], i)
64-
}
65-
```
60+
```javascript
61+
let numToIndex = new Map()
62+
63+
for (let i = 0; i < nums.length; i++) {
64+
numToIndex.set(nums[i], i)
65+
}
66+
```
6667

6768
2. Traverse the array, if `target - num` is in `Map`, return it. Otherwise, add `num` to `Map`.
68-
```javascript
69-
let numToIndex = new Map()
7069

71-
for (let i = 0; i < nums.length; i++) {
72-
if (numToIndex.has(target - nums[i])) { // 1
73-
return [numToIndex.get(target - nums[i]), i] // 2
74-
}
75-
76-
numToIndex.set(nums[i], i)
77-
}
78-
```
70+
```javascript
71+
let numToIndex = new Map()
72+
73+
for (let i = 0; i < nums.length; i++) {
74+
if (numToIndex.has(target - nums[i])) { // 1
75+
return [numToIndex.get(target - nums[i]), i] // 2
76+
}
77+
78+
numToIndex.set(nums[i], i)
79+
}
80+
```
7981

8082
### Complexity
8183
* Time: `O(n)`.

en/1-1000/15-3sum.md

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ Notice that the order of the output and the order of the triplets does not matte
3636

3737
### [Constraints]
3838
- `3 <= nums.length <= 3000`
39-
- `-10**5 <= nums[i] <= 10**5`
39+
- `-10^5 <= nums[i] <= 10^5`
4040

4141
### [Hints]
4242
<details>
4343
<summary>Hint 1</summary>
44-
So, we essentially need to find three numbers x, y, and z such that they add up to the given value. If we fix one of the numbers say x, we are left with the two-sum problem at hand!
44+
So, we essentially need to find three numbers `x`, `y`, and `z` such that they add up to the given value. If we fix one of the numbers say `x`, we are left with the two-sum problem at hand!
4545
</details>
4646

4747
<details>
4848
<summary>Hint 2</summary>
49-
For the two-sum problem, if we fix one of the numbers, say x, we have to scan the entire array to find the next number y, which is value - x where value is the input parameter. Can we change our array somehow so that this search becomes faster?
49+
For the two-sum problem, if we fix one of the numbers, say `x`, we have to scan the entire array to find the next number `y`, which is `value - x` where value is the input parameter. Can we change our array somehow so that this search becomes faster?
5050
</details>
5151

5252
<details>
@@ -55,7 +55,7 @@ Notice that the order of the output and the order of the triplets does not matte
5555
</details>
5656

5757
## Intuition
58-
1. The `sum` of three numbers equals `0`, which is equivalent to the `sum` of _two numbers_ equaling the _**negative** third number_.
58+
1. The `sum` of three numbers equals `0`, which is equivalent to the `sum` of *two numbers* equaling the ***negative** third number*.
5959
2. There are two options:
6060
1. First `determine two numbers` and `find the third number`
6161
2. First `determine one number` and `find the other two numbers`
@@ -64,30 +64,33 @@ Notice that the order of the output and the order of the triplets does not matte
6464
5. For `option 1`, only the `Python` sample code is given. This article focuses on `option 2`.
6565

6666
## Steps
67+
6768
1. Sort `nums`.
6869
2. Iterate over `nums`.
6970
3. pseudocode:
70-
```javascript
71-
for (i = 0; i < nums.length; i++) {
72-
left = i + 1
73-
right = nums.length - 1
74-
75-
while (left < right) {
76-
if (condition1) {
77-
left += 1
78-
} else (condition2) {
79-
right -= 1
80-
}
81-
}
82-
}
83-
```
71+
72+
```javascript
73+
for (i = 0; i < nums.length; i++) {
74+
left = i + 1
75+
right = nums.length - 1
76+
77+
while (left < right) {
78+
if (condition1) {
79+
left += 1
80+
} else (condition2) {
81+
right -= 1
82+
}
83+
}
84+
}
85+
```
8486

8587
## Complexity
8688
* Time: `O(N * N)`.
8789
* Space: `O(N)`.
8890

8991
## Python
9092
### Solution 1: Use Map (complex and slow)
93+
9194
```python
9295
# from collections import defaultdict
9396

en/1-1000/18-4sum.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
LeetCode link: [18. 4Sum](https://leetcode.com/problems/4sum), difficulty: **Medium**
33

44
## LeetCode description of "18. 4Sum"
5-
Given an array `nums` of `n` integers, return _an array of all the **unique** quadruplets_ `[nums[a], nums[b], nums[c], nums[d]]` such that:
5+
Given an array `nums` of `n` integers, return *an array of all the **unique** quadruplets* `[nums[a], nums[b], nums[c], nums[d]]` such that:
66

7-
* `0 <= a, b, c, d < n`
8-
* `a`, `b`, `c`, and `d` are **distinct**.
9-
* `nums[a] + nums[b] + nums[c] + nums[d] == target`
10-
* You may return the answer in **any order**.
7+
- `0 <= a, b, c, d < n`
8+
- `a`, `b`, `c`, and `d` are **distinct**.
9+
- `nums[a] + nums[b] + nums[c] + nums[d] == target`
10+
- You may return the answer in **any order**.
1111

1212
### [Example 1]
1313
**Input**: `nums = [1,0,-1,0,-2,2], target = 0`
@@ -21,8 +21,8 @@ Given an array `nums` of `n` integers, return _an array of all the **unique** qu
2121

2222
### [Constraints]
2323
- `1 <= nums.length <= 200`
24-
- `-10**9 <= nums[i] <= 10**9`
25-
- `-10**9 <= target <= 10**9`
24+
- `-10^9 <= nums[i] <= 10^9`
25+
- `-10^9 <= target <= 10^9`
2626

2727
## Intuition
2828
1. The idea of this question is the same as [15. 3Sum](15-3sum.md), please click the link to view.
@@ -31,7 +31,7 @@ Given an array `nums` of `n` integers, return _an array of all the **unique** qu
3131
4. You may have already seen that no matter it is `two numbers`, `three numbers` or `four numbers`, the `Two Pointers Technique` can be used.
3232

3333
## Complexity
34-
* Time: `O(N**3)`.
34+
* Time: `O(N^3)`.
3535
* Space: `O(N)`.
3636

3737
## Python

en/1-1000/19-remove-nth-node-from-end-of-list.md

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,45 @@ Given the `head` of a linked list, remove the `n-th` node from the end of the li
4141

4242
## Steps
4343
1. First find out `node_count`.
44-
```ruby
45-
node_count = 0
46-
node = head
4744

48-
while node
49-
node_count += 1
50-
node = node.next
51-
end
52-
```
45+
```ruby
46+
node_count = 0
47+
node = head
48+
49+
while node
50+
node_count += 1
51+
node = node.next
52+
end
53+
```
5354

5455
2. When `index == node_count - N`, delete the node by `node.next = node.next.next`.
55-
```ruby
56-
index = 0
57-
node = head
5856

59-
while node
60-
if index == node_count - n
61-
node.next = node.next.next
62-
break
63-
end
64-
65-
index += 1
66-
node = node.next
67-
end
68-
```
57+
```ruby
58+
index = 0
59+
node = head
60+
61+
while node
62+
if index == node_count - n
63+
node.next = node.next.next
64+
break
65+
end
66+
67+
index += 1
68+
node = node.next
69+
end
70+
```
6971

7072
3. Since the deleted node may be `head`, a virtual node `dummy_node` is used to facilitate unified processing.
71-
```ruby
72-
dummy_head = ListNode.new # 1
73-
dummy_head.next = head # 2
74-
node = dummy_head # 3
7573

76-
# omitted code
77-
78-
return dummy_head.next
79-
```
74+
```ruby
75+
dummy_head = ListNode.new # 1
76+
dummy_head.next = head # 2
77+
node = dummy_head # 3
78+
79+
# omitted code
80+
81+
return dummy_head.next
82+
```
8083

8184
## Complexity
8285
* Time: `O(N)`.

0 commit comments

Comments
 (0)