力扣 15. 三数之和

快排 + 双指针

  1. 快排
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def quick_sort(left, right):
if left >= right: return
i, j = left, right

while i < j:
while nums[j] > nums[left] and i < j:
j -= 1
while nums[i] <= nums[left] and i < j:
i += 1

nums[i], nums[j] = nums[j], nums[i]
nums[i], nums[left] = nums[left], nums[i]

quick_sort(left, i - 1)
quick_sort(i + 1, right)
  1. 双指针(三指针)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
res = []
for k in range(0, num_len - 2):
if nums[k] > 0:
break
if k > 0 and nums[k] == nums[k - 1]: continue
i, j = k + 1, num_len - 1
while i < j:
cur = nums[k] + nums[i] + nums[j]
if cur > 0:
j -= 1
while i < j and nums[j] == nums[j + 1]: j -= 1
elif cur < 0:
i += 1
while i < j and nums[i] == nums[i - 1]: i += 1
else:
res.append([nums[k], nums[i], nums[j]])
i += 1
j -= 1
while i < j and nums[i] == nums[i - 1]: i += 1
while i < j and nums[j] == nums[j + 1]: j -= 1

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors:2837 | Views:3164
  • 小站在各种崩坏中坚持了: 1910天3小时1分44秒

请我喝杯咖啡吧~

支付宝
微信