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
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`.
8
10
9
11
Notice that the solution set must not contain duplicate triplets.
10
12
11
13
### [Example 1]
14
+
12
15
**Input**: `nums = [-1,0,1,2,-1,-4]`
13
16
14
17
**Output**: `[[-1,-1,2],[-1,0,1]]`
15
18
16
-
**Explanation**:
19
+
**Explanation**:
20
+
17
21
```
18
22
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
19
23
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
23
27
```
24
28
25
29
### [Example 2]
30
+
26
31
**Input**: `nums = [0,1,1]`
27
32
28
33
**Output**: `[]`
29
34
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
+
```
31
40
32
41
### [Example 3]
42
+
33
43
**Input**: `nums = [0,0,0]`
34
44
35
45
**Output**: `[[0,0,0]]`
36
46
37
47
**Explanation**: `The only possible triplet sums up to 0.`
38
48
39
49
### [Constraints]
50
+
40
51
-`3 <= nums.length <= 3000`
41
52
-`-10^5 <= nums[i] <= 10^5`
42
53
43
54
### [Hints]
55
+
44
56
<details>
45
57
<summary>Hint 1</summary>
46
58
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
+
47
61
</details>
48
62
49
63
<details>
50
64
<summary>Hint 2</summary>
51
65
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
+
52
68
</details>
53
69
54
70
<details>
55
71
<summary>Hint 3</summary>
56
72
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
+
57
75
</details>
58
76
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.
61
80
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`.
67
86
68
87
## Steps
69
88
@@ -87,11 +106,60 @@ Notice that the order of the output and the order of the triplets does not matte
87
106
```
88
107
89
108
## 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.
Copy file name to clipboardExpand all lines: en/1-1000/160-intersection-of-two-linked-lists.md
+24-39
Original file line number
Diff line number
Diff 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**.
3
6
4
7
## LeetCode description of "160. Intersection of Two Linked Lists"
8
+
5
9
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`.
6
10
7
11
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
13
17
**Note** that the linked lists must **retain their original structure** after the function returns.
**Input**: `intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4]`
26
32
27
33
**Output**: `Intersected at '2'`
28
34
29
35
### [Example 3]
36
+
30
37

31
38
32
39
**Input**: `listA = [2,6,4], listB = [1,5]`
33
40
34
41
**Output**: `No intersection`
35
42
36
43
### [Constraints]
44
+
37
45
- The number of nodes of `listA` is in the `m`.
38
46
- The number of nodes of `listB` is in the `n`.
39
47
-`1 <= m, n <= 3 * 10^4`
40
48
-`1 <= Node.val <= 10^5`
41
49
50
+
<br>
42
51
**Follow up**: Could you write a solution that runs in `O(m + n)` time and use only `O(1)` memory?
43
52
44
53
## Intuition
54
+
45
55
This is a typical "encounter" problem, and it is best to transform it into a real-life scenario to enhance understanding.
46
56
47
57
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.
48
58
49
59
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)
50
60
61
+
<details><summary>Click to view the answer</summary><p>
51
62
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.
52
63
53
64
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`.
54
65
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.
55
66
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>
56
68
57
69
## Steps
70
+
58
71
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`.
59
72
60
73
```python
@@ -95,10 +108,12 @@ You must first calculate how many kilometers her home is farther from the school
95
108
```
96
109
97
110
## Complexity
98
-
* Time: `O(m + n)`.
99
-
* Space: `O(1)`.
111
+
112
+
- Time complexity: `O(m + n)`.
113
+
- Space complexity: `O(1)`.
100
114
101
115
## Java
116
+
102
117
```java
103
118
/**
104
119
* public class ListNode {
@@ -154,6 +169,7 @@ public class Solution {
154
169
```
155
170
156
171
## Python
172
+
157
173
```python
158
174
# class ListNode:
159
175
# def __init__(self, x):
@@ -195,12 +211,8 @@ class Solution:
195
211
returnNone
196
212
```
197
213
198
-
## C++
199
-
```cpp
200
-
// Welcome to create a PR to complete the code of this language, thanks!
201
-
```
202
-
203
214
## JavaScript
215
+
204
216
```javascript
205
217
/**
206
218
* Definition for singly-linked list.
@@ -251,6 +263,7 @@ var getIntersectionNode = function (headA, headB) {
251
263
```
252
264
253
265
## C#
266
+
254
267
```c#
255
268
/**
256
269
* public class ListNode {
@@ -310,37 +323,9 @@ public class Solution
310
323
}
311
324
```
312
325
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
337
327
338
-
## Rust
339
-
```rust
328
+
```java
340
329
// Welcome to create a PR to complete the code of this language, thanks!
341
330
```
342
331
343
-
## Other languages
344
-
```
345
-
// Welcome to create a PR to complete the code of this language, thanks!
0 commit comments