From 472b57a40e611b2c7f76d82352c9f8657a580b2e Mon Sep 17 00:00:00 2001 From: Chen-Wang-JY <75002576+Chen-Wang-JY@users.noreply.github.com> Date: Fri, 21 May 2021 17:10:59 +0800 Subject: [PATCH] =?UTF-8?q?Update=200015.=E4=B8=89=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加了Python3的双指针法 --- problems/0015.三数之和.md | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 96dc1ac3..9875f037 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -218,8 +218,32 @@ class Solution { ``` 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 func threeSum(nums []int)[][]int{