Advertisement
lazar955

Untitled

Oct 22nd, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. func threeSum(nums []int) [][]int {
  2. var res [][]int
  3. sort.Ints(nums)
  4.  
  5. for i:=0;i<len(nums);i++ {
  6. if i>0 && nums[i] == nums[i - 1] {
  7. continue // skip same element
  8. }
  9. l,r:=i+1, len(nums) -1
  10. for l<r {
  11. sum:= nums[i] + nums[l] + nums[r]
  12. if sum < 0 {
  13. l++
  14. }else if sum > 0 {
  15. r--
  16. }else {
  17. res = append(res, []int{nums[i],nums[l],nums[r]})
  18. for l<r && nums[l] == nums[l+1] {l++}
  19. l++
  20. r--
  21. }
  22. }
  23. }
  24. return res
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement