leetcode刷题python之四数之和

class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        nums.sort()
        length=len(nums)
        result=[]
        if length<4:
            return []
        for i in range(length-3):
            if i>0 and nums[i-1]==nums[i]:
                continue
            if nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target:
                break
            if nums[i]+nums[length-3]+nums[length-2]+nums[length-1] 1 and nums[j-1]==nums[j]:
                    continue
                if nums[i]+nums[j]+nums[j+1]+nums[j+2]>target:
                    break
                if nums[j]+nums[length-3]+nums[length-2]+nums[length-1]

虽然自己也可以写一写简单的双指针,但是优化做的还是不够好,大神的优化太厉害了,首先是相同的数跳过,这个是之前也用过的,关键是要搞懂为什么在range()里面的坐标,length-2和length-3都可以,而-3可以用到进一步的优化之中

你可能感兴趣的:(leetcode_python)