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).
给一个整型数组,找出三个数使其和尽可能的接近给定的数,返回三个数的和
注意题目有个假设对于每个输入都有一个解决方法,与3Sum差不多
class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
sort(num.begin(),num.end());
int closet = num[]+num[]+num[];
for(int i = ; i < num.size()-; ++ i){
int start = i+, end = num.size()-,sum = ;
while(start < end){
sum = num[i] + num[start] + num[end];
if(sum == target) return sum;
else if(sum > target) end--;
else start++;
closet = abs(sum - target) < abs(closet-target) ? sum : closet;
}
}
return closet;
}
};

 

Leetcode 3Sum Closest的更多相关文章

  1. [LeetCode]3Sum Closest题解

    3sum Closest: Given an array S of n integers, find three integers in S such that the sum is closest ...

  2. [LeetCode] 3Sum Closest 最近三数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  3. LeetCode 3Sum Closest (Two pointers)

    题意 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  4. LeetCode——3Sum Closest

    Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...

  5. leetcode 3Sum Closest python

    class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List ...

  6. LeetCode 3Sum Closest 最近似的3sum(2sum方法)

    题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...

  7. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  8. LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum

    1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...

  9. 3Sum Closest - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 3Sum Closest - LeetCode 注意点 和3Sum那道题的target是0,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...

随机推荐

  1. 为何JAVA虚函数(虚方法)会造成父类可以"访问"子类的假象?

      首先,来看一个简单的JAVA类,Base. 1 public class Base { 2 String str = "Base string"; 3 protected vo ...

  2. 创建一个自定义颜色IRgbColor

    后续文章需要用到,很简单的一个小函数 /// <summary> /// 自定义颜色 /// </summary> /// <param name="r&quo ...

  3. es_Linux

    命令:echo 显示.印出 echo $PATH 通过  "su - vbird"" 这个指令来变换到 vbird身份 权限: rwx rwx rwx 以4 2 1数字表 ...

  4. css清楚浮动的方法

  5. Pandas-数据整理

    Pandas包对数据的常用整理功能,相当于数据预处理(不包括特征工程) 目录 丢弃值 drop() 缺失值处理 isnull() & notnull() dropna() fillna() 值 ...

  6. 【PHP升级】CentOS6.3编译安装 PHP5.4.38

    先前安装的PHP5.3.28(参考:CentOS6.3编译安装Nginx1.4.7 + MySQL5.5.25a + PHP5.3.28),现在准备升级PHP到5.4.38,有如下几个地方需要重新编译 ...

  7. 第2月第24天 coretext 行高

    1.NSMutableAttributedString 行高 NSMutableAttributedString *attributedString = [[NSMutableAttributedSt ...

  8. exception catch doesn't work?? (python 3)

    exception catch doesn't work?? (python 3) except u.URLError, e: ^ SyntaxError: invalid syntax in Pyt ...

  9. ps批量处理图片

    刚刚有朋友问,ps咋做批量动作呢,其实特别简单,基本一劳永逸,用尺寸做个例子,大家看看就知道了.

  10. word20161206

    D-channel / D 信道 DACL, discretionary access control list / 自由访问控制列表 daily backup / 每日备份 Data Communi ...