Skip to content

Commit 936bf5b

Browse files
committed
3Sum and 4Sum
Follow up 2Sum problem. How to solve 3Sum and 4Sum problem? What’s their relationships? Could we get some generic idea to solve this kind of problems? I give the explanation here.
1 parent a792197 commit 936bf5b

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

3Sum and 4Sum/README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@ Find all unique triplets in the array which gives the sum of zero.
66

77
**Note**: The solution set must not contain duplicate triplets.
88

9-
> For example, given array S = [-1, 0, 1, 2, -1, -4](),
10-
> A solution set is:
11-
> [
12-
> ]()> [-1, 0, 1](),
13-
> [-1, -1, 2]()
14-
> ]
9+
> For example, given array S = [-1, 0, 1, 2, -1, -4],
10+
> A solution set is: [[-1, 0, 1], [-1, -1, 2]]
1511
1612
### Solution
1713
Like [2Sum][5], we can sort the array first, then use 2 pointers method to solve the problem. But here, we need 3 numbers, so we need add one more pointer. But how to play with 3 pointers?
@@ -45,4 +41,4 @@ I will write another topic to present how we will solve this KSum problem with a
4541

4642

4743

48-
[5]: https://github.com/raywenderlich/swift-algorithm-club/tree/master/Two-Sum%20Problem
44+
[5]: https://github.com/raywenderlich/swift-algorithm-club/tree/master/Two-Sum%20Problem

0 commit comments

Comments
 (0)