Python simple short solution


  • 0
    A
    class Solution(object):
        def threeSumClosest(self, nums, target):
    
            res = float('inf')
            nums.sort()
            
            for i in xrange(len(nums)-2):
    
                l, r = i+1, len(nums)-1
                while l < r:
                    s = nums[i] + nums[l] + nums[r] - target
                    res = min(s,res,key=abs)
                    l+= s<=0; r-= s>0
            
            return res+target
            
            
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.