Swift solution - Two Pointers


  • 0
    class Solution {
        func threeSumClosest(_ nums: [Int], _ target: Int) -> Int {
            if nums.count < 3 {
                return 0
            }
            
            let count = nums.count
            var result = nums[0] + nums[1] + nums[count - 1]
            let nums = nums.sorted()
            var left = 0
            var right = 0
            var sum = 0
            
            for i in 0..<(count - 2) {
                left = i + 1
                right = count - 1
                while left < right {
                    sum = nums[i] + nums[left] + nums[right]
                    if sum > target {
                        right -= 1
                    } else {
                        left += 1
                    }
                    if abs(sum - target) < abs(result - target) {
                        result = sum
                    }
                }
            }
            
            return result
        }
    }
    

Log in to reply
 

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