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 + 1 = 0 不也接近 1 吗?不行,因为人家要求3数之和.

思路同15题 3Sum. 既固定i, 让 lo = i + 1; hi = len - 1;

关键是对sum == ta, sum > ta, sum < ta的处理,以及退出while(lo < hi)之后的处理.

人家想法,咱代码:

\(O(n^2)\) time, \(O(1)\) extra space.

int threeSumClosest(vector<int>& A, int ta) {
const int n = A.size();
int min = INT_MAX; // 存放最接近ta的三个数和
sort(A.begin(), A.end()); for (int i = 0; i < n - 2; i++) {
// 剔除连续值
if (i > 0 && A[i] == A[i - 1]) continue;
int lo = i + 1, hi = n - 1; // lo==hi 不可以,sum = A[i] + A[lo] + A[hi]会出问题
while (lo < hi) {
int sum = A[i] + A[lo] + A[hi]; // 保存最接近ta的3数之和
min = abs(sum - ta) < abs(min) ? (sum - ta) : min; if (sum == ta) return ta;
else if (sum > ta) hi--;
else lo++;
}
}
return min + ta; // min = sum - ta, we ask sum now!
}

16. 3Sum Closest(中等)的更多相关文章

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

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

  2. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  3. 《LeetBook》leetcode题解(16):3Sum Closest [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  4. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  5. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  6. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  7. 15. 3Sum、16. 3Sum Closest和18. 4Sum

    15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...

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

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  9. Leetcode 16. 3Sum Closest

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

随机推荐

  1. 大数据学习总结(7)we should...

    大数据场景一.各种标签查询 查询要素:人.事.物.单位 查询范围:A范围.B范围.... 查询结果:pic.name.data from 1.痛点:对所有文本皆有实时查询需求2.难点:传统SQL使用W ...

  2. Ubuntu下安装最新sublime

    1. Install the GPG key: wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key ...

  3. Centos:如何查找安装的jdk的目录

    使用$JAVA_HOME的话能定位JDK的安装路径的前提是配置了环境变量$JAVA_HOME,否则如下所示,根本定位不到JDK的安装路径. 正确的方式是通过 which java: [tt@vddd ...

  4. POJ-3255 Roadblocks---Dijkstra队列优化+次短路

    题目链接: https://vjudge.net/problem/POJ-3255 题目大意: 给无向图,求1到n的次短路长度 思路: 由于边数较多,应该使用dijkstra的队列优化 用d数组存储最 ...

  5. ZOJ-2913 Bus Pass---BFS进阶版

    题目链接: https://vjudge.net/problem/ZOJ-2913 题目大意: 问哪个区域到公交路线上所有区域的最大距离最小 思路: 这里要求出到底是哪个区域到某些指定区域的最大距离最 ...

  6. 复习HTML+CSS(6)

    n  表格和表单的嵌套顺序 n  单行文本域 语法格式:<input type="text" 属性="值"> 常用属性 l  Name:文本框的名字 ...

  7. 盒子浮动float

    一.float的基本规律 规律1: 标准流模型中的块级盒子,默认宽度100%: 而浮动的块级盒子,宽度不会自动伸展,而是由内容(文字.padding)撑开: 浮动后的行级元素,可以设置宽度高度等属性. ...

  8. Opencv在mac系统的安装与试用

    1.在mac终端内,使用brew安装opencv3,这时我的opencv被安装到/usr/local/Cellar/opencv3/3.2.0内. 2.新建xcode 项目,选择command lin ...

  9. [LeetCode] Minimum Index Sum of Two Lists 两个表单的最小坐标和

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

  10. kafka知识体系-kafka设计和原理分析-kafka leader选举

    kafka leader选举 一条消息只有被ISR中的所有follower都从leader复制过去才会被认为已提交.这样就避免了部分数据被写进了leader,还没来得及被任何follower复制就宕机 ...