LeetCode: 16-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:
Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Constraints:
3 <= nums.length <= 10^3
-10^3 <= nums[i] <= 10^3
-10^4 <= target <= 10^4
這個題目跟 15 題非常相似,同樣是給定一組整數陣列輸入,不過還多給了一個目標值 target。我們需要找出 3 個數值,使其加總值越接近 target 越好。
需要注意的是,我們要返回的並不是找到的陣列,而是直接返回最接近 target 的數值。
Read More »LeetCode: 16-3Sum Closest 解題紀錄