LeetCode : 15.3sum

参考了:

https://www.cnblogs.com/zuoyuan/p/3699159.html

但是里面的子while循环,来判断相邻两个数相等其实可以不要,因为在第一个while 里面判断了。这样可以提高运算速度。

class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        nums.sort()
        sums = 0
        list_all = []
        if len(nums) < 3:
                return []
        for i in range(len(nums) -2):
                if i == 0  or nums[i] > nums[i-1]:
                        point_left = i + 1 ; point_right = len(nums) - 1
                        while point_right > point_left:
                                sums = nums[i] + nums[point_left] + nums[point_right]
                                if sums == 0:
                                        list_all.append([nums[i], nums[point_left], nums[point_right]])
                                        point_left += 1; point_right -= 1
                                        while point_left < point_right and nums[point_left] == nums[point_left-1] :
                                                point_left += 1
                                        while point_left < point_right and nums[point_right] == nums[point_right+1] :
                                                point_right -= 1
                                elif sums < 0:
                                        #while point_right > point_left:
                                        #       point_left += 1
                                        #       if nums[point_left] > nums[point_left-1]: break
                                        point_left += 1
                                else:
                                        point_right -= 1
                                        #while point_right > point_left:
                                        #       point_right -= 1
                                        #       if nums[point_right] < nums[point_right+1]: break
        return list_all
~
~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值