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
Copy file name to clipboardExpand all lines: dsa-solutions/lc-solutions/0000-0099/0015-3sum.md
+17-13Lines changed: 17 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,10 @@ tags:
6
6
- Hash Table
7
7
- Math
8
8
- String
9
+
- python
10
+
- javascript
11
+
- java
12
+
- c++
9
13
description: find the number of triplets with given sum .
10
14
sidebar_position: 15
11
15
---
@@ -16,7 +20,7 @@ Given an integer array `nums`, return all the triplets `[nums[i], nums[j], nums[
16
20
17
21
Notice that the solution set must not contain duplicate triplets.
18
22
19
-
### Example 1
23
+
### Example 1:
20
24
21
25
-**Input:**`nums = [-1,0,1,2,-1,-4]`
22
26
-**Output:**`[[-1,-1,2],[-1,0,1]]`
@@ -27,13 +31,13 @@ Notice that the solution set must not contain duplicate triplets.
27
31
- The distinct triplets are `[-1,0,1]` and `[-1,-1,2]`.
28
32
- Notice that the order of the output and the order of the triplets does not matter.
29
33
30
-
### Example 2
34
+
### Example 2:
31
35
32
36
-**Input:**`nums = [0,1,1]`
33
37
-**Output:**`[]`
34
38
-**Explanation:** The only possible triplet does not sum up to 0.
35
39
36
-
### Example 3
40
+
### Example 3:
37
41
38
42
-**Input:**`nums = [0,0,0]`
39
43
-**Output:**`[[0,0,0]]`
@@ -237,23 +241,23 @@ public:
237
241
res = []
238
242
```
239
243
240
-
2.**Sort the Input Array:**
244
+
2.**Sort the Input Array:**
241
245
242
246
- Sort the input array `nums` in non-decreasing order.
243
247
244
248
```python
245
249
nums.sort()
246
250
```
247
251
248
-
3.**Iterate Through the Array:**
252
+
3.**Iterate Through the Array:**
249
253
250
254
- Iterate through each element in the sorted array `nums`.
251
255
252
256
```python
253
257
for i inrange(len(nums)):
254
258
```
255
259
256
-
4.**Skip Duplicate Elements:**
260
+
4.**Skip Duplicate Elements:**
257
261
258
262
- Check if the current element is a duplicate of the previous element and skip it if it is.
259
263
@@ -262,7 +266,7 @@ public:
262
266
continue
263
267
```
264
268
265
-
5.**Initialize Pointers:**
269
+
5.**Initialize Pointers:**
266
270
267
271
- Initialize two pointers `j` and `k` to point to the elements next to the current element `i` and at the end of the array, respectively.
268
272
@@ -271,23 +275,23 @@ public:
271
275
k =len(nums) -1
272
276
```
273
277
274
-
6.**Two-Pointer Approach:**
278
+
6.**Two-Pointer Approach:**
275
279
276
280
- Use a two-pointer approach with pointers `j` and `k` to find triplets whose sum equals zero.
277
281
278
282
```python
279
283
while j < k:
280
284
```
281
285
282
-
7.**Calculate Total:**
286
+
7.**Calculate Total:**
283
287
284
288
- Calculate the total sum of the current triplet.
285
289
286
290
```python
287
291
total = nums[i] + nums[j] + nums[k]
288
292
```
289
293
290
-
8.**Adjust Pointers Based on Total:**
294
+
8.**Adjust Pointers Based on Total:**
291
295
292
296
- If the total sum is greater than zero, decrement the `k` pointer to decrease the total sum.
293
297
@@ -311,7 +315,7 @@ public:
311
315
j +=1
312
316
```
313
317
314
-
9.**Handle Duplicate Triplets:**
318
+
9.**Handle Duplicate Triplets:**
315
319
316
320
- Increment the `j` pointer to skip any duplicate elements.
317
321
@@ -320,12 +324,12 @@ public:
320
324
j +=1
321
325
```
322
326
323
-
10.**Return Result:**
327
+
10.**Return Result:**
324
328
- Return the list `res` containing all the unique triplets whose sum is zero.
325
329
```python
326
330
return res
327
331
```
328
332
329
333
This algorithm efficiently finds all unique trip
330
334
331
-
lets in the given array `nums` whose sum equals zero using a two-pointer approach. It avoids duplicate triplets by skipping duplicate elements during traversal.
335
+
lets in the given array `nums` whose sum equals zero using a two-pointer approach. It avoids duplicate triplets by skipping duplicate elements during traversal.
0 commit comments