zoukankan      html  css  js  c++  java
  • 10 3SumClosest

    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 nums = [-1, 2, 1, -4], and target = 1.

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

    最基本的暴力搜索法。。。

    class Solution {
    public:
        int threeSumClosest(vector<int>& nums, int target) {
            
            int sum = 0;
            int d_min = INT_MAX;
            int len = nums.size();
            
            for(int i = 0;i < len; i++ )
            {
                for(int j = i + 1 ;j< len; j++)
                {
                    for( int k = j+1; k<len; k++)
                    {
                        int temp = nums[i] + nums[j] + nums[k];
                        int d = abs(temp - target);
                            
                            
                        if(d < d_min)
                        {
                            d_min = d;
                            sum = temp;
                            
                        }
                        else;
                    }
                }
            }
            
            
            
            return sum;
            
        }
    };
    

    比较快速的方法是使用 排序 + 两头逼近法, 下面算法用时约8ms

    class Solution {
    public:
        int threeSumClosest(vector<int>& nums, int target) {
            
            int len = nums.size();
            
            sort(nums.begin(), nums.end());
            
            int min = nums[0]+nums[1]+nums[2];
            int max = nums[len-1]+nums[len-2]+nums[len-3];
            
            if(target < min) return min;
            if(target > max) return max;
            
            
            //from two side -> center
            
            int left,right;
            int d_min = INT_MAX;
            int sum = 0;
            
            for(int i=0;i<len;i++)
            {
                left = i+1;
                right = len -1 ;
                
                while(left<right)
                {
                    int temp = nums[i] + nums[left] + nums[right];
                    int d_curr = abs(temp - target);
                    
                    if(d_curr < d_min)
                    {
                        d_min = d_curr;
                        sum = temp; 
                    }
                    
                    if(temp < target) left++;
                    else if(temp == target) return target;
                    else right--;
                    
                }
            }
            
            return sum;
            
        }
    };
    
  • 相关阅读:
    BZOJ3312: [Usaco2013 Nov]No Change
    BZOJ1750: [Usaco2005 qua]Apple Catching
    BZOJ2733: [HNOI2012]永无乡
    BZOJ4756: [Usaco2017 Jan]Promotion Counting
    PHP 反射机制Reflection
    NOD 1113矩阵快速幂
    CODEVS 3500
    hdu 5172 GTY's gay friends 线段树
    LA 4329 Ping pong
    hdu 3500 DFS(限定)
  • 原文地址:https://www.cnblogs.com/xiaoyisun06/p/11378953.html
Copyright © 2011-2022 走看看