腾讯//最接近的三数之和

给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。

例如,给定数组 nums = [-1,2,1,-4], 和 target = 1.

与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).

参考三数之和,可以用类似方法解决此题;首先进行排序,因为要用到two pointer来遍历找两数之和,只有在从小到大排序之后的结果上,才能根据情况移动left和right。当确定好第一个数字之后,就在剩下的array中找两数之和,再加上第一个数字,用这个temp_sum减去target来得到temp_diff,如果temp_diff比之前的小,那么更新diff和closestSum。利用two pointer 的性质继续调整即可。

class Solution {
    public int threeSumClosest(int[] nums, int target) {
        Arrays.sort(nums);
        int closestSum = 0;
        int diff = Integer.MAX_VALUE;
        
        for(int i=0;i<nums.length-2;i++){
            int left = i+1;
            int right = nums.length-1;
            while(left<right){
                int temp_sum = nums[left]+nums[right]+nums[i];
                int temp_diff = Math.abs(temp_sum-target);
                if(temp_diff<diff){
                    closestSum = temp_sum;
                    diff = temp_diff;
                }
                if(temp_sum < target)
                    left++;
                else if(temp_sum > target)
                    right--;
                else
                    return temp_sum;
            }
            
        }
        return closestSum;
    }
    
}
class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        vector<vector<int>> res;
        sort(nums.begin(),nums.end());
        int size = nums.size();
        int start,end,temp_sum,temp_diff,diff=0x7fffffff;
        int t = 0;
        for(int i = 0;i < size-2;i++){
            start = i+1;
            end = size-1;
            temp_sum = 0 - nums[i];
            while(start<end){
                temp_sum= nums[start]+nums[end]+nums[i];
                int temp_diff = abs(temp_sum-target);
                if(temp_diff<diff){
                    t = temp_sum;
                    diff = temp_diff;
                }
                if(temp_sum<target)
                    start++;
                else if(temp_sum>target)
                    end--;
                
                else
                    return temp_sum;
            }
        }
        return t; 
    }
};

 

posted @ 2018-10-22 09:50  strawqqhat  阅读(156)  评论(0编辑  收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }