3Sum Closest leetcode

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).我的思路:    (1)首先对给定的数组进行排序,让里面的数值从小到大;    (2)遍历数组,用一个for循环控制作为外循环,下标从小到大进行遍历,因为需要3个数,所以用外循环的这个下标对应的数组值作为base,然后再        初始化一个begin作为base的下一位置,end作为数组末尾,三者相加得到sum,若是value=sum-target(目标值)更小则保存sum的值,并且若value<0        则begin++,若value>o则end--,若是value=0,则返回target;看代码:
class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target);
    int  findClosest(vector<int>& nums, int base, int end, int target);
private:
    int result, finalResult, gap = 99999999, gap1 = 99999999;
};
int Solution::threeSumClosest(vector<int>& nums, int target){
    //int result,gap=99999999;
    sort(nums.begin(), nums.end());
    int len = nums.size();
    for (int i = 0; i < nums.size() - 2; i++)
    {
        //cout << "before" << endl;
        int res1 = findClosest(nums, i, len - 1, target);
        if (res1 < gap1)
            gap1 = res1;
        finalResult = result;
    }
    //    cout << "over" << endl;
    //    return gap1;
    return finalResult;

}
int Solution::findClosest(vector<int>& nums, int base, int end, int target){
    //cout << "executing" << endl;
    int begin = base + 1;
    while (begin < end){
        int value = nums[base] + nums[begin] + nums[end] - target;
        int temp = abs(nums[base] + nums[begin] + nums[end] - target);
        if (temp <= gap){
            //        int s = nums[base], s2 = nums[begin], s3 = nums[end];
            gap = abs(nums[base] + nums[begin] + nums[end] - target);
            result = nums[base] + nums[begin] + nums[end];
            if (value == 0)
                return 0;
            else if (value>0)
                end--;
            else if (value < 0)
                begin++;
        }
        else {
            if (value>0) end--;
            else begin++;
        //    return gap;
        }
    }
    return gap;
}
    
时间: 2024-08-28 13:51:00

3Sum Closest leetcode的相关文章

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

3Sum Closest——LeetCode

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

3Sum Closest Leetcode Python

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

[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

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