Easy python solution


  • 0
    M
    class Solution(object):
        def threeSumClosest(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: int
            """
            nums.sort()
            sm =None
            
            
            for i in range(len(nums)-2):
                j = i+1
                k = len(nums)-1
                while j<k:
                    s = nums[i]+nums[j]+nums[k] 
                    if s == target:
                        return target
                    elif s<target:
                        if sm==None or abs(target-s) < abs(target-sm):
                            sm = s
                            
                        j+=1
                    elif s>target:
                        if sm==None or abs(target-s) < abs(target-sm):
                            sm = s
                        k = k-1
     
            return sm
                    
                    
            
    
    

Log in to reply
 

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