mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0015.三数之和.md
This commit is contained in:
@ -298,61 +298,72 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
(版本一) 双指针
|
||||||
```Python
|
```Python
|
||||||
class Solution:
|
class Solution:
|
||||||
def threeSum(self, nums):
|
def threeSum(self, nums: List[int]) -> List[List[int]]:
|
||||||
ans = []
|
result = []
|
||||||
n = len(nums)
|
|
||||||
nums.sort()
|
nums.sort()
|
||||||
# 找出a + b + c = 0
|
|
||||||
# a = nums[i], b = nums[left], c = nums[right]
|
for i in range(len(nums)):
|
||||||
for i in range(n):
|
# 如果第一个元素已经大于0,不需要进一步检查
|
||||||
left = i + 1
|
|
||||||
right = n - 1
|
|
||||||
# 排序之后如果第一个元素已经大于零,那么无论如何组合都不可能凑成三元组,直接返回结果就可以了
|
|
||||||
if nums[i] > 0:
|
if nums[i] > 0:
|
||||||
break
|
return result
|
||||||
if i >= 1 and nums[i] == nums[i - 1]: # 去重a
|
|
||||||
|
# 跳过相同的元素以避免重复
|
||||||
|
if i > 0 and nums[i] == nums[i - 1]:
|
||||||
continue
|
continue
|
||||||
while left < right:
|
|
||||||
total = nums[i] + nums[left] + nums[right]
|
left = i + 1
|
||||||
if total > 0:
|
right = len(nums) - 1
|
||||||
right -= 1
|
|
||||||
elif total < 0:
|
while right > left:
|
||||||
|
sum_ = nums[i] + nums[left] + nums[right]
|
||||||
|
|
||||||
|
if sum_ < 0:
|
||||||
left += 1
|
left += 1
|
||||||
|
elif sum_ > 0:
|
||||||
|
right -= 1
|
||||||
else:
|
else:
|
||||||
ans.append([nums[i], nums[left], nums[right]])
|
result.append([nums[i], nums[left], nums[right]])
|
||||||
# 去重逻辑应该放在找到一个三元组之后,对b 和 c去重
|
|
||||||
while left != right and nums[left] == nums[left + 1]: left += 1
|
# 跳过相同的元素以避免重复
|
||||||
while left != right and nums[right] == nums[right - 1]: right -= 1
|
while right > left and nums[right] == nums[right - 1]:
|
||||||
left += 1
|
|
||||||
right -= 1
|
right -= 1
|
||||||
return ans
|
while right > left and nums[left] == nums[left + 1]:
|
||||||
|
left += 1
|
||||||
|
|
||||||
|
right -= 1
|
||||||
|
left += 1
|
||||||
|
|
||||||
|
return result
|
||||||
```
|
```
|
||||||
Python (v3):
|
(版本二) 使用字典
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def threeSum(self, nums: List[int]) -> List[List[int]]:
|
def threeSum(self, nums: List[int]) -> List[List[int]]:
|
||||||
if len(nums) < 3: return []
|
result = []
|
||||||
nums, res = sorted(nums), []
|
nums.sort()
|
||||||
for i in range(len(nums) - 2):
|
# 找出a + b + c = 0
|
||||||
cur, l, r = nums[i], i + 1, len(nums) - 1
|
# a = nums[i], b = nums[j], c = -(a + b)
|
||||||
if res != [] and res[-1][0] == cur: continue # Drop duplicates for the first time.
|
for i in range(len(nums)):
|
||||||
|
# 排序之后如果第一个元素已经大于零,那么不可能凑成三元组
|
||||||
while l < r:
|
if nums[i] > 0:
|
||||||
if cur + nums[l] + nums[r] == 0:
|
break
|
||||||
res.append([cur, nums[l], nums[r]])
|
if i > 0 and nums[i] == nums[i - 1]: #三元组元素a去重
|
||||||
# Drop duplicates for the second time in interation of l & r. Only used when target situation occurs, because that is the reason for dropping duplicates.
|
continue
|
||||||
while l < r - 1 and nums[l] == nums[l + 1]:
|
d = {}
|
||||||
l += 1
|
for j in range(i + 1, len(nums)):
|
||||||
while r > l + 1 and nums[r] == nums[r - 1]:
|
if j > i + 2 and nums[j] == nums[j-1] == nums[j-2]: # 三元组元素b去重
|
||||||
r -= 1
|
continue
|
||||||
if cur + nums[l] + nums[r] > 0:
|
c = 0 - (nums[i] + nums[j])
|
||||||
r -= 1
|
if c in d:
|
||||||
|
result.append([nums[i], nums[j], c])
|
||||||
|
d.pop(c) # 三元组元素c去重
|
||||||
else:
|
else:
|
||||||
l += 1
|
d[nums[j]] = j
|
||||||
return res
|
return result
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
Reference in New Issue
Block a user