human
I found an interesting problem on https://leetcode.com/problems/3sum-closest/:
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:

Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

I tried it in Python, but could not do it. Can you solve it?

Give me a Python code to find three integers in nums such that the sum is closest to target?
assistant
#Here is Python code to find three integers in nums such that the sum is closest to target.

class Solution:
     def threeSumClosest(self, nums, target):
         """
         :type nums: List[int]
         :type target: int
         :rtype: int
         """
         if len(nums) < 3:
             return sum(nums)
         elif len(nums) == 3:
             return sum(nums)
         nums = sorted(nums)
         res = nums[0]+nums[1]+nums[2]
         for i in range(len(nums) - 2):
             if i!= 0 and nums[i] == nums[i-1]:
                 continue
             j = i+1
             k = len(nums) - 1
             while j < k:
                 s = nums[i] + nums[j] + nums[k]
                 if s == target:
                     return target
                 if s < target:
                     j += 1
                 else:
                     k -= 1                
                 if abs(s-target) < abs(res-target):
                     res = s
         return res