mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Update 0015.三数之和.md
增加了Python3的双指针法
This commit is contained in:
@ -218,8 +218,32 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```class Solution:
|
||||||
|
def threeSum(self, nums):
|
||||||
|
ans = []
|
||||||
|
n = len(nums)
|
||||||
|
nums.sort()
|
||||||
|
for i in range(n):
|
||||||
|
left = i + 1
|
||||||
|
right = n - 1
|
||||||
|
if nums[i] > 0:
|
||||||
|
break
|
||||||
|
if i >= 1 and nums[i] == nums[i - 1]:
|
||||||
|
continue
|
||||||
|
while left < right:
|
||||||
|
total = nums[i] + nums[left] + nums[right]
|
||||||
|
if total > 0:
|
||||||
|
right -= 1
|
||||||
|
elif total < 0:
|
||||||
|
left += 1
|
||||||
|
else:
|
||||||
|
ans.append([nums[i], nums[left], nums[right]])
|
||||||
|
while left != right and nums[left] == nums[left + 1]: left += 1
|
||||||
|
while left != right and nums[right] == nums[right - 1]: right -= 1
|
||||||
|
left += 1
|
||||||
|
right -= 1
|
||||||
|
return ans
|
||||||
|
```
|
||||||
Go:
|
Go:
|
||||||
```Go
|
```Go
|
||||||
func threeSum(nums []int)[][]int{
|
func threeSum(nums []int)[][]int{
|
||||||
|
Reference in New Issue
Block a user