Leetcode[15]三数之和

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010870545/article/details/88856672
class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        nums = sorted(nums)
        N = len(nums)
        if N < 3: return []
        seen = set()
        for i, n in enumerate(nums):
            if i > 0 and nums[i-1] == nums[i]:continue
            lo, hi = i+1, N-1
            while lo < hi:
                s = nums[lo]+nums[i]+nums[hi]
                if s > 0:
                    hi -= 1
                elif s < 0:
                    lo += 1
                else:
                    seen.add((nums[i], nums[lo], nums[hi]))
                    lo+=1
                    hi-=1
        return sorted(seen)

猜你喜欢

转载自blog.csdn.net/u010870545/article/details/88856672