@GregHogg

Master Data Structures & Algorithms for FREE at https://AlgoMap.io/

@user-jm6gp2qc8x

whoever gave the title to this problem knew what he was doing lol

@hbhavsi

Wow, this is the clearest explanation of the solution I have come across that explains constraints with the correct example, rather than just the default example, and I am left scratching my head at the final solution, like huh, why was this needed at all? Thanks so much!

@anna-plink

One of my favorite solutions for a problem so far 👍

@christianjt7018

I liked this solution a lot more than the previous one, keep up the amazing work!

@WatchNoah

instead of the two while loops in the summ == 0, you can actually also simply use a set as answer and add sorted tuples. this also has a runtime of 558ms

@bokistotel

Thanks, for anyone interested in 2 pointer solution using SET() to store repetition in GO:
NOTE 1 - WHY SORTING? 
Removing SORTING  invalidates the logic that i++ makes the sum bigger and j-- makes the sum smaller!!

If we find solution ex (-3,1,2), that means that "left" cannot be "1" anymore and "right" cannot be "2" anymore. BUT "left"      CAN be "2" and "right" CAN be "1". If we sort it that basically ensures that in current run (for fixed c) this wont happen. And you can safely increment/decrement pointers. "left" will skip all 1s, and "right" will skip all 2s

NOTE 2 - I know GO kindof sucks for Leetcode because there is not built in set() data structure, you have to use map.

NOTE 3 - you can add logic to check if current "c" pointer is equal to previous, if it is, move to the next. But that does not influence the result speed that much

import (
    "sort"
)
func threeSum(nums []int) [][]int {
	sort.Ints(nums)
	c := 0
	i := c + 1 // left
	j := len(nums) - 1 //right
	var result [][]int

	var triplet [3]int
	uniqueTriplets := make(map[[3]int]struct{}) //storing triplets

	for c < len(nums)-2 { 
		for i < j {
			total := nums[c] + nums[i] + nums[j]
			triplet = [3]int{nums[c], nums[i], nums[j]}

			if total == 0 {
				if _, exists := uniqueTriplets[triplet]; !exists {
					uniqueTriplets[triplet] = struct{}{}
					result = append(result, []int{nums[c], nums[i], nums[j]})
				}
				i++
				j--
			} else if total < 0 {
				i++
			} else {
				j--
			}
		}
                // increment c, and reset i & j. Here you can implement "skip" logic to increment c at value different from previous (current)
                c++
		i = c + 1
		j = len(nums) - 1
	}
	return result
}

@pravinprince3221

Thank you Greg for the wonderful video, it is so helpful for me and my team, thanks again

@akshaypendyala

One small thing to note that, in the sorted array if we reach the number zero or a positive number we can stop the program as we know the positives don't add up to make sum as zero.

@malakggh

That was a very good explanation!

@kavan3

I'd recommend solving Two Sum and Two Sum II before approaching this problem

@venkateshkannan7398

Best explanation👌

@Ondrej-jf1er

I do not solve this after two hour, even with help of your grafical explanation, i feel like shit. 🙁

@mehdismaeili3743

Excellent .

@mahmoudelazb8028

What about using set instead of filtration that we do?

Keep in course you'r good instructor

@thomasthedankengine5400

What about the time complexity that came from sorting? Wouldn't it be like nlog(n) times n²?

@flynnblu6992

Love your videos! But puzzled... after finding the first answer, isn't nums[hi + 1] out of range on the following, given hi starts out as (n - 1), hence we are checking nums[n]?

while hi > lo and nums[hi] == nums[hi + 1]:

@Nechss

is using sort in the spirit of these problems?

@user-jm6gp2qc8x

greg i dont think hashmap 3sum video you've done