3Sum Closest

思路:先对vector进行排序,然后夹逼计算,时间复杂度O(n^2),里面需要注意在判断完边界后,先计算thres和result,然后处理下标,这里不需要考虑重复情况。

class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        sort(nums.begin(), nums.end());

        int sum = 0;
        int thres = INT_MAX;
        int result = nums[0] + nums[1] + nums[2];
        for(int i=0; i<nums.size()-2; ++i)
        {
            int j = i+1;
            int k = nums.size() - 1;

            while(j<k)
            {
                if(nums[i] + nums[j] + nums[k] < target)
                {
                    if(fabs(nums[i] + nums[j] + nums[k] - target) < thres)
                    {
                        thres = fabs(nums[i] + nums[j] + nums[k] - target);
                        result = nums[i] + nums[j] + nums[k];
                    }
                    ++j;
                }
                else if(nums[i] + nums[j] + nums[k] > target)
                {
                    if(fabs(nums[i] + nums[j] + nums[k] - target) < thres)
                    {
                        thres = fabs(nums[i] + nums[j] + nums[k] - target);
                        result = nums[i] + nums[j] + nums[k];
                    }
                    --k;
                }
                else
                {
                    return nums[i] + nums[j] + nums[k];
                }
            }
        }
        return result;
    }
};
时间: 2024-10-22 12:20:00

3Sum Closest的相关文章

No.016 3Sum Closest

16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium 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

[LeetCode][JavaScript]3Sum Closest

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 arr

LeetCode 016 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 = {

[leetcode]3Sum Closest @ Python

原题地址:http://oj.leetcode.com/problems/3sum-closest/ 题意:数组中每三个元素进行求和,找出所有和中大小最接近target的和,并返回这个和与target之间的差值. 解题思路:使用一个变量mindiff来监测和与target之间的差值,如果差值为0,直接返回sum值. 代码: class Solution: # @return an integer def threeSumClosest(self, num, target): num.sort()

leetcode 3Sum 3Sum Closest 4Sum

这几个题很典型也是国外一些知名公司经常会问到的题 3Sum: 排序,避免重复,时间复杂度O(n^2) class Solution { public: vector<vector<int> > threeSum(vector<int> &num) { int len=num.size(); sort(num.begin(),num.begin()+len); vector<vector<int> > ret; ret.clear(); i

LeetCode[Array]----3Sum Closest

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 arr

2Sum,3Sum,4Sum,kSum,3Sum Closest系列

1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于,j左移,否则为其中一个解 3.时间复杂度:O(nlgn)+O(n) 4.空间:O(1) 5.代码: void twoSum(vector<int>& nums,int numsSize,int target,vector<vector<int>>& two

leetcode_16题——3Sum Closest(两个指针)

3Sum Closest Total Accepted: 38536 Total Submissions: 143223My Submissions Question Solution 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 ma

leetCode 16.3Sum Closest (离给定值最近的三数之和) 解题思路和方法

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 arr

3Sum Closest leetcode java

题目: 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 = {-