Skip to content

Commit

Permalink
Time: 30 ms (37.61%), Space: 9.6 MB (60.69%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hovanhoa committed Feb 22, 2025
1 parent f1c6642 commit f85e7b5
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions 0015-3sum/0015-3sum.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
func threeSum(nums []int) [][]int {
ans := [][]int{}
sort.Slice(nums, func(i, j int) bool {
return nums[i] < nums[j]
})

ans := [][]int{}

for i := 0; i < len(nums) - 2; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue
Expand All @@ -14,6 +15,7 @@ func threeSum(nums []int) [][]int {
sum := nums[i] + nums[l] + nums[r]
if sum == 0 {
ans = append(ans, []int{nums[i], nums[l], nums[r]})

l += 1
for l < r && nums[l] == nums[l-1] {
l += 1
Expand All @@ -24,7 +26,6 @@ func threeSum(nums []int) [][]int {
r -= 1
}
}

}

return ans
Expand Down

0 comments on commit f85e7b5

Please sign in to comment.