Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func threeSum(nums []int) [][]int {
- var res [][]int
- sort.Ints(nums)
- for i:=0;i<len(nums);i++ {
- if i>0 && nums[i] == nums[i - 1] {
- continue // skip same element
- }
- l,r:=i+1, len(nums) -1
- for l<r {
- sum:= nums[i] + nums[l] + nums[r]
- if sum < 0 {
- l++
- }else if sum > 0 {
- r--
- }else {
- res = append(res, []int{nums[i],nums[l],nums[r]})
- for l<r && nums[l] == nums[l+1] {l++}
- l++
- r--
- }
- }
- }
- return res
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement