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. GPS模块数据放入谷歌地图显示,不准

    GPS 串口读出的是 DDMM.MMMM格式 一般上位机是 DD.DDDDDD°或 DD°MM'SS" 格式, 这两种都可以在 GE 里直接输入 举例说明: 3147.8749 (示例,经纬 ...

  2. Java Web笔记之Servlet(1)

    今天在学习Servlet时,使用浏览器显示的网页效果与预期的有差异,仔细查找发现实<!DOCTYPE>声明的问题,截图如下: 代码如下: package secondServlet; im ...

  3. LoadRunner 函数之lr_xml_get_values

    简单实例如 char *xml_input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>&q ...

  4. Spring task定时任务

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  5. tyvj1203 机器分配

    描述 总公司拥有高效生产设备M台,准备分给下属的N个公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配这M台设备才能使国家得到的盈利最大?求出最大盈利值.其中M<=100,N& ...

  6. nyoj 623 A*B Problem II(矩阵)

    A*B Problem II 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 ACM的C++同学有好多作业要做,最头痛莫过于线性代数了,因为每次做到矩阵相乘的时候,大 ...

  7. 【bzoj4720】[NOIP2016]换教室

    题目描述 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程.在可以选择的课程中,有2n节课程安排在n个时间段上.在第i(1≤i≤n)个时间段上,两节内容相同的课程同时在不同的 ...

  8. T-SQL 语句的优化

    SQL调优. 1.索引是数据库调优的最根本的优化方法.聚簇索引.非聚簇索引. 聚簇索引:物理序与索引顺序相同.(只能有一个) 非聚簇索引:物理顺序与索引顺序不相同. 2.调整WHERE 子句中的连接顺 ...

  9. CSS3——transform学习

    CSS动画效果可以使用transform和Animation,前者较简单,先学习前者. transform有几个基本变换,平移.旋转.缩放.扭曲 一.translate平移 有translate2d和 ...

  10. php libevent 扩展使用示例

    <?php define()); define()); class epoll{ private static $socket; public static $connections; priv ...