Very easy to understand Swift solution O(n^2)


  • 0
    func threeSumClosest(nums: [Int], _ target: Int) -> Int {
        let nums = nums.sort()
        var sum = Int.max/2
        for i in 0..<nums.count {
            var tmp = 0
            var left = i + 1, right = nums.count - 1
            while left < right {
                tmp = nums[i] + nums[left] + nums[right]
                if tmp < target {
                    left += 1
                }else {
                    right -= 1
                }
                sum = abs(tmp - target) < abs(sum - target) ? tmp : sum
            }
        }
        return sum
    }
    

Log in to reply
 

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