LeetCode OJ:3Sum Closest(最接近的三数之和)

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

    For example, given array S = {-1 2 1 -4}, and target = 1.

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

求三个数的和sum,使sum与target相差最小。这题实际上跟前面坐的2Sum,以及3Sum相差不是很大,大体的想法是先排序,然后外层的for循环遍历从小到大的数,然后从剩下的数中取三数之和,然后把最小的一步步保存下来,最终将最小的返回就行了。代码如下:
 1 class Solution {
 2 public:
 3     int threeSumClosest(vector<int>& nums, int target) {
 4         int sz = nums.size();
 5         sort(nums.begin(), nums.end());//当然了,首先还是要排序的
 6         int diff = INT_MAX;//这个直接取INT_MAX,这样计算出来的diff一定是比这个要小的
 7         int tmpDiff;
 8         int ans;
 9         int beg, end;
10         for (int i = 0; i < sz - 2; ++i){
11             beg = i + 1, end = sz - 1;
12             while (beg < end){
13                 int sum = nums[i] + nums[beg] + nums[end];
14                 if ((tmpDiff = abs(sum - target)) < diff){
15                     diff = tmpDiff;
16                     ans = sum;
17                 }
18                 if (sum > target){
19                     end--;
20                 }
21                 else if (sum < target){
22                     beg++;
23                 }
24                 else
25                     return sum;//相等的话就直接返回了
26             }
27         }
28         return ans;
29     }
30 };

大体上就是这样

时间: 2024-08-08 10:24:44

LeetCode OJ:3Sum Closest(最接近的三数之和)的相关文章

[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: Given array nu

LeetCode:最接近的三数之和【16】

LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 题目分析 这道题就是三数之和问题的一种变形. 三数之和问题的求解策略是将三指针变为双指针问题

两数之和,三数之和,最接近的三数之和,四数之和

LeetCode有一系列做法套路相同的题目,N数之和就可以算一个 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 第一个解决办法,简单暴力,堆for循环就是,但

LeetCode16.最接近的三数之和 JavaScript

给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 答案参考 /** * @param {number[]} nums * @param {number} target * @return {num

[Swift]LeetCode16. 最接近的三数之和 | 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 nu

[leetcode 16] 最接近的三数之和

给定一个包括n的整数数组nums,和一个目标值target.找出nums中的三个整数,使得它们的和与target最接近,返回这三个数的和.假定每次输入只存在一个答案. 示例: nums = [-1,2,1,-4] 和 target = 1 与 target 最接近的三个数的和为2. (-1+2+1=2) 代码: class Solution(object): def threeSumClosest(self,nums,target): """ :type nums: List

每天AC系列(二):最接近的三数之和

1 题目 leetcode第16题,给定一个数组与一个目标数,找出数组中其中的三个数,这三个数的和要与目标数最接近. 2 暴力 按惯例先来一次O(n3)的暴力: int temp = nums[0]+nums[1]+nums[2]; for(int i=0;i<nums.length;++i) for(int j=i+1;j<nums.length;++j) for(int k=j+1;k<nums.length;++k) { int temp1 = nums[i]+nums[j]+nu

16.最接近的三数之和

给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). class Solution {    public int threeSumClosest(int[] nums, int target) { 

16. 最接近的三数之和 O(N^2)

给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 思路就是先枚举一个数字,然后用尺取确定另外两个数字. class Solution { public: int threeSumClosest(v