Leetcode 16: 3Sum Closest

class Solution:
    def threeSumClosest(self, nums: List[int], target: int) -> int:
        res=0
        temp=float('inf')
        nums.sort()
        n=len(nums)
        #[-3, 0, 1, 2]
        for i in range(n-2):
            l, r= i+1, n-1
            while labs(target-s):
                        temp=abs(target-s)
                        res=s; l+=1
                    elif temp<=abs(target-s):
                        l+=1
                    while ltarget:
                    if temp>abs(target-s):
                        temp=abs(target-s)
                        res=s; r-=1
                    elif temp<=abs(target-s):
                        r-=1
                    while l

 

你可能感兴趣的:(Leetcode 16: 3Sum Closest)