Skip to content

Commit

Permalink
Time: 30 ms (38.09%), Space: 8.7 MB (99.91%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hovanhoa committed Jan 17, 2025
1 parent 15e35e3 commit 911a9ff
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions 0015-3sum/0015-3sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ func threeSum(nums []int) [][]int {
if i > 0 && nums[i] == nums[i-1] {
continue
}

l, r := i + 1, len(nums) - 1
for l < r {
total := nums[i] + nums[l] + nums[r]
if total < 0 {
l += 1
} else if total > 0 {
r -= 1
} else {
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
}
} else if sum < 0 {
l += 1
} else {
r -= 1
}
}

}

return ans
Expand Down

0 comments on commit 911a9ff

Please sign in to comment.