Skip to content

Commit 08fb407

Browse files
committed
Added and updated 52 files.
1 parent a1f980e commit 08fb407

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2445
-2540
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ You can return the answer in any order.
2020

2121
**Explanation**:
2222

23+
```
2324
Because nums[0] + nums[1] == 9, we return [0, 1].
25+
```
2426

2527
### [Example 2]
2628

@@ -321,3 +323,4 @@ class Solution:
321323
```java
322324
// Welcome to create a PR to complete the code of this language, thanks!
323325
```
326+

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

+80-43
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
Original link: [leetcoder.net - LeetCoder: Fucking Good LeetCode Solutions](https://leetcoder.net/en/leetcode/15-3sum)
22

33
# 15. 3Sum - LeetCoder: Fucking Good LeetCode Solutions
4+
45
LeetCode link: [15. 3Sum](https://leetcode.com/problems/3sum), Difficulty: **Medium**.
56

67
## LeetCode description of "15. 3Sum"
8+
79
Given an integer array nums, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j`, `i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`.
810

911
Notice that the solution set must not contain duplicate triplets.
1012

1113
### [Example 1]
14+
1215
**Input**: `nums = [-1,0,1,2,-1,-4]`
1316

1417
**Output**: `[[-1,-1,2],[-1,0,1]]`
1518

16-
**Explanation**:
19+
**Explanation**:
20+
1721
```
1822
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
1923
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
@@ -23,47 +27,62 @@ Notice that the order of the output and the order of the triplets does not matte
2327
```
2428

2529
### [Example 2]
30+
2631
**Input**: `nums = [0,1,1]`
2732

2833
**Output**: `[]`
2934

30-
**Explanation**: `The only possible triplet does not sum up to 0.`
35+
**Explanation**:
36+
37+
```
38+
The only possible triplet does not sum up to 0.
39+
```
3140

3241
### [Example 3]
42+
3343
**Input**: `nums = [0,0,0]`
3444

3545
**Output**: `[[0,0,0]]`
3646

3747
**Explanation**: `The only possible triplet sums up to 0.`
3848

3949
### [Constraints]
50+
4051
- `3 <= nums.length <= 3000`
4152
- `-10^5 <= nums[i] <= 10^5`
4253

4354
### [Hints]
55+
4456
<details>
4557
<summary>Hint 1</summary>
4658
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!
59+
60+
4761
</details>
4862

4963
<details>
5064
<summary>Hint 2</summary>
5165
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?
66+
67+
5268
</details>
5369

5470
<details>
5571
<summary>Hint 3</summary>
5672
The second train of thought for two-sum is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search?
73+
74+
5775
</details>
5876

59-
## Intuition
60-
1. The `sum` of three numbers equals `0`, which is equivalent to the `sum` of *two numbers* equaling the ***negative** third number*.
77+
## Intuition 1
78+
79+
1. The `sum` of three numbers equals `0`, which is equivalent to the `sum` of *two numbers* equaling the ***negative*** third number.
6180
2. There are two options:
62-
1. First `determine two numbers` and `find the third number`
63-
2. First `determine one number` and `find the other two numbers`
64-
3. If you choose `option 1`, you need to use `Map`. Because you need to deduplicate `nums`; when searching for the third number in `Map`, you also need to avoid the two numbers that have been determined, which is more troublesome to implement.
65-
4. If you choose `option 2`, you need to use the `two pointers` algorithm when searching for the other two numbers.
66-
5. For `option 1`, only the `Python` sample code is given. This article focuses on `option 2`.
81+
- Option 1. First `determine one number`, and then `find the other two numbers`.
82+
- Option 2. First `determine two numbers`, and then `find the third number`.
83+
3. If you choose `option 2`, you need to use `Map`. Because you need to deduplicate `nums`; when searching for the third number in `Map`, you also need to avoid the two numbers that have been determined, which is more troublesome to implement.
84+
4. If you choose `option 1`, you need to use the `two pointers` algorithm when searching for the other two numbers.
85+
5. For `option 2`, only the `Python` sample code is given. This article focuses on `option 1`.
6786

6887
## Steps
6988

@@ -87,11 +106,60 @@ Notice that the order of the output and the order of the triplets does not matte
87106
```
88107

89108
## Complexity
109+
110+
- Time complexity: `O(N * N)`.
111+
- Space complexity: `O(N)`.
112+
113+
## Python
114+
115+
```python
116+
# If you want the program to run faster, uncomment the two places in the code.
117+
class Solution:
118+
def threeSum(self, nums: List[int]) -> List[List[int]]:
119+
nums.sort()
120+
# nums_2 = []
121+
# for i, num in enumerate(nums):
122+
# if i >= 3 and num == nums[i - 1] == nums[i - 2] == nums[i - 3]:
123+
# continue
124+
# nums_2.append(num)
125+
# nums = nums_2
126+
results = set()
127+
128+
for i, num in enumerate(nums[:len(nums) - 2]):
129+
# if num > 0:
130+
# break
131+
left = i + 1
132+
right = len(nums) - 1
133+
134+
while left < right:
135+
sum_ = nums[left] + nums[right]
136+
if sum_ == -num:
137+
results.add((num, nums[left], nums[right]))
138+
left += 1
139+
elif sum_ > -num:
140+
right -= 1
141+
else:
142+
left += 1
143+
144+
return list(results)
145+
```
146+
147+
## Other languages
148+
149+
```java
150+
// Welcome to create a PR to complete the code of this language, thanks!
151+
```
152+
153+
## Intuition 2
154+
155+
Please refer to the idea of `​​​​Solution 1`. Here we only give the code to explain why using `Map` is not a good idea.
156+
157+
## Complexity
158+
90159
- Time complexity: `O(N * N)`.
91160
- Space complexity: `O(N)`.
92161

93162
## Python
94-
### Solution 1: Use Map (complex and slow)
95163

96164
```python
97165
# from collections import defaultdict
@@ -133,40 +201,9 @@ def duplicate_removed_nums(nums):
133201
return new_nums
134202
```
135203

136-
### Solution 2: Two pointers (recommended)
137-
```python
138-
# If you want the program to run faster, uncomment the two places in the code.
139-
class Solution:
140-
def threeSum(self, nums: List[int]) -> List[List[int]]:
141-
nums.sort()
142-
# nums_2 = []
143-
# for i, num in enumerate(nums):
144-
# if i >= 3 and num == nums[i - 1] == nums[i - 2] == nums[i - 3]:
145-
# continue
146-
# nums_2.append(num)
147-
# nums = nums_2
148-
results = set()
149-
150-
for i, num in enumerate(nums[:len(nums) - 2]):
151-
# if num > 0:
152-
# break
153-
left = i + 1
154-
right = len(nums) - 1
155-
156-
while left < right:
157-
sum_ = nums[left] + nums[right]
158-
if sum_ == -num:
159-
results.add((num, nums[left], nums[right]))
160-
left += 1
161-
elif sum_ > -num:
162-
right -= 1
163-
else:
164-
left += 1
165-
166-
return list(results)
167-
```
168-
169204
## Other languages
205+
170206
```java
171207
// Welcome to create a PR to complete the code of this language, thanks!
172208
```
209+

en/1-1000/160-intersection-of-two-linked-lists.md

+24-39
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
# 160. Intersection of Two Linked Lists - Best Practices of LeetCode Solutions
2-
LeetCode link: [160. Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists), difficulty: **Easy**.
1+
Original link: [leetcoder.net - LeetCoder: Fucking Good LeetCode Solutions](https://leetcoder.net/en/leetcode/160-intersection-of-two-linked-lists)
2+
3+
# 160. Intersection of Two Linked Lists - LeetCoder: Fucking Good LeetCode Solutions
4+
5+
LeetCode link: [160. Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists), Difficulty: **Easy**.
36

47
## LeetCode description of "160. Intersection of Two Linked Lists"
8+
59
Given the heads of two singly linked-lists `headA` and `headB`, return _the node at which the two lists intersect_. If the two linked lists have no intersection at all, return `null`.
610

711
For example, the following two linked lists begin to intersect at node `c1`:
@@ -13,48 +17,57 @@ The test cases are generated such that there are **no cycles** anywhere in the e
1317
**Note** that the linked lists must **retain their original structure** after the function returns.
1418

1519
### [Example 1]
20+
1621
![](../../images/examples/160_1.png)
1722

1823
**Input**: `listA = [4,1,8,4,5], listB = [5,6,1,8,4,5]`
1924

2025
**Output**: `Intersected at '8'`
2126

2227
### [Example 2]
28+
2329
![](../../images/examples/160_2.png)
2430

2531
**Input**: `intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4]`
2632

2733
**Output**: `Intersected at '2'`
2834

2935
### [Example 3]
36+
3037
![](../../images/examples/160_3.png)
3138

3239
**Input**: `listA = [2,6,4], listB = [1,5]`
3340

3441
**Output**: `No intersection`
3542

3643
### [Constraints]
44+
3745
- The number of nodes of `listA` is in the `m`.
3846
- The number of nodes of `listB` is in the `n`.
3947
- `1 <= m, n <= 3 * 10^4`
4048
- `1 <= Node.val <= 10^5`
4149

50+
<br>
4251
**Follow up**: Could you write a solution that runs in `O(m + n)` time and use only `O(1)` memory?
4352

4453
## Intuition
54+
4555
This is a typical "encounter" problem, and it is best to transform it into a real-life scenario to enhance understanding.
4656

4757
Suppose you are A, and you are pursuing B. The destination is school. On the way to school, the distance at the back is what you both have to go through, and the distance at the front is for each of you to go your own way. The node spacing is assumed to be one kilometer.
4858

4959
Now, one morning, you both finished breakfast at the same time and are going to ride your bike to school. And you have a goal: to meet B and chat with him/her for a few words. What will you do? (Take Example 1 as an example)
5060

61+
<details><summary>Click to view the answer</summary><p>
5162
You must first calculate how many kilometers her home is farther from the school than your home, and then wait for her to walk these kilometers before setting off. As long as you have the same speed, you will definitely meet, and the node where you meet is the answer.
5263

5364
1. First calculate the number of nodes in the two linked lists A and B. The number of nodes in linked list A is `node_count_a`, and the number of nodes in linked list B is `node_count_b`.
5465
2. If `node_count_b > node_count_a`, then perform `node = node.next` for `node_count_b - node_count_a` times on linked list B.
5566
3. At this time, repeat `node = node.next` on the two linked lists until the same node is found or one of the linked lists has reached the end.
67+
</p></details>
5668

5769
## Steps
70+
5871
1. First calculate the number of nodes in the two linked lists A and B. The number of nodes in linked list A is `node_count_a`, and the number of nodes in linked list B is `node_count_b`.
5972

6073
```python
@@ -95,10 +108,12 @@ You must first calculate how many kilometers her home is farther from the school
95108
```
96109

97110
## Complexity
98-
* Time: `O(m + n)`.
99-
* Space: `O(1)`.
111+
112+
- Time complexity: `O(m + n)`.
113+
- Space complexity: `O(1)`.
100114

101115
## Java
116+
102117
```java
103118
/**
104119
* public class ListNode {
@@ -154,6 +169,7 @@ public class Solution {
154169
```
155170

156171
## Python
172+
157173
```python
158174
# class ListNode:
159175
# def __init__(self, x):
@@ -195,12 +211,8 @@ class Solution:
195211
return None
196212
```
197213

198-
## C++
199-
```cpp
200-
// Welcome to create a PR to complete the code of this language, thanks!
201-
```
202-
203214
## JavaScript
215+
204216
```javascript
205217
/**
206218
* Definition for singly-linked list.
@@ -251,6 +263,7 @@ var getIntersectionNode = function (headA, headB) {
251263
```
252264

253265
## C#
266+
254267
```c#
255268
/**
256269
* public class ListNode {
@@ -310,37 +323,9 @@ public class Solution
310323
}
311324
```
312325

313-
## Go
314-
```go
315-
// Welcome to create a PR to complete the code of this language, thanks!
316-
```
317-
318-
## Ruby
319-
```ruby
320-
# Welcome to create a PR to complete the code of this language, thanks!
321-
```
322-
323-
## C
324-
```c
325-
// Welcome to create a PR to complete the code of this language, thanks!
326-
```
327-
328-
## Kotlin
329-
```kotlin
330-
// Welcome to create a PR to complete the code of this language, thanks!
331-
```
332-
333-
## Swift
334-
```swift
335-
// Welcome to create a PR to complete the code of this language, thanks!
336-
```
326+
## Other languages
337327

338-
## Rust
339-
```rust
328+
```java
340329
// Welcome to create a PR to complete the code of this language, thanks!
341330
```
342331

343-
## Other languages
344-
```
345-
// Welcome to create a PR to complete the code of this language, thanks!
346-
```

0 commit comments

Comments
 (0)