<p>Given an integer array <code>nums</code> of length <code>n</code> and an integer <code>target</code>, find three integers in <code>nums</code> such that the sum is closest to <code>target</code>.</p>

<p>Return <em>the sum of the three integers</em>.</p>

<p>You may assume that each input would have exactly one solution.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [-1,2,1,-4], target = 1
<strong>Output:</strong> 2
<strong>Explanation:</strong> The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,0,0], target = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> The sum that is closest to the target is 0. (0 + 0 + 0 = 0).
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= nums.length &lt;= 500</code></li>
	<li><code>-1000 &lt;= nums[i] &lt;= 1000</code></li>
	<li><code>-10<sup>4</sup> &lt;= target &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution:
    def threeSumClosest(self, nums: List[int], target: int) -> int:
        #sort nums
        #memory the closest
        #iterate nums, pick pivot at i from nums
        #then scan it's right side
        #l = i+1, r=len(nums)-1
        #get cur sum of i, l, r and update closest
        #if the sum < target, move l to the right
        #if the sum > target, move r to the left
        #if sum==target break the loop

        nums = sorted(nums)
        closest_dist = float("inf")
        closest_sum = 0
        for i,v in enumerate(nums):
            l = i+1
            r = len(nums)-1
            while l<r:
                cur_sum = nums[i] + nums[l] + nums[r]
                if abs(cur_sum - target) < closest_dist:
                    closest_sum = cur_sum
                    closest_dist = abs(cur_sum - target)
                if cur_sum < target: l+=1
                elif cur_sum > target: r-=1
                else:#exact target
                    break
        return closest_sum
16600