59-最接近的三数之和

给一个包含 n 个整数的数组 S, 找到和与给定整数 target 最接近的三元组,返回这三个数的和。

注意事项

只需要返回三元组之和,无需返回三元组本身

样例

例如 S = [-1, 2, 1, -4] and target = 1. 和最接近 1 的三元组是 -1 + 2 + 1 = 2.

挑战

O(n^2) 时间, O(1) 额外空间。

标签

排序 数组 两根指针

思路

延续三数之和的思路,实时更新最接近的三数和

code

class Solution {
public:
/**
* @param numbers: Give an array numbers of n integer
* @param target: An integer
* @return: return the sum of the three integers, the sum closest target.
*/
int threeSumClosest(vector<int> nums, int target) {
// write your code here
int size = nums.size();
if(size < 3) {
return 0;
} sort(nums.begin(),nums.end());
int ans = 0x7FFFFFFF, i = 0, j = 0, k = 0; for(i=0; i<size; i++) {
for(j=i+1, k=size-1; j<k;){
int sum = nums[i] + nums[j] + nums[k]; ans = abs(target-sum) < abs(target-ans) ? sum : ans; if(sum > target) {
k--;
}
else if(sum < target) {
++j;
}
else {
return sum;
}
}
}
return ans;
}
};

lintcode-59-最接近的三数之和的更多相关文章

  1. Leetcode题库——16.最接近的三数之和

    @author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...

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

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

  3. LeetCode:最接近的三数之和【16】

    LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...

  4. Java实现 LeetCode 16 最接近的三数之和

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

  5. Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/63802fda72be45eba98d9e4 ...

  6. LeetCode-016-最接近的三数之和

    最接近的三数之和 题目描述:给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只 ...

  7. 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 ...

  8. 最接近的三数之和(给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数, 使得它们的和与 target 最接近。返回这三个数的和)

    例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 思路:首先对数组进行排序     ...

  9. 最接近的三数之和(java实现)

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

随机推荐

  1. vue里使用百度地图

    最近公司项目里要用到百度地图,然后查阅了一些资料,并总结了下: 首先呢,由于本公司使用的是百度离线地图,那么我们首先需要将百度地图离线包放置static静态文件目录下(我的离线地图包名是“baidu_ ...

  2. eclipse 突然debug模式不能正常运行了

    eclipse 突然debug模式不能正常运行了,但非debug模式却能正常运行.debug模式不能正常启动的现象描述如下:    点击eclipse debug按钮,console窗口显示tomca ...

  3. 【2018 ICPC南京网络赛 A】An Olympian Math Problem(数论题)

    Alice, a student of grade 6, is thinking about an Olympian Math problem, but she feels so despair th ...

  4. ABAP术语-Application Server

    Application Server 原文:http://www.cnblogs.com/qiangsheng/archive/2007/12/17/1002777.html Server that ...

  5. 构建高可靠hadoop集群之4-保全模式

    本文主要翻译自http://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-common/SecureMode.html 译注:之所以 ...

  6. js | javascript获取和设置元素的属性

    获取和设置元素的内容: var nv = document.getElementById("pid"); alert(nv.innerHTML); nv.innerHTML=&qu ...

  7. Linux下文件字符编码格式检测和转换

    目前多数情况下, 我们遇到的非英文字符文件都是使用UTF-8编码的, 这时一般我们查看这些文件的内容都不会有问题. 不过有时, 我们有可能会遇到非UTF-8编码的文件, 比如中文的GBK编码, 或者俄 ...

  8. svg在html的使用

    <svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'> <defs&g ...

  9. hadoop生态搭建(3节点)-03.zookeeper配置

    # https://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase8-2177648.html # ===== ...

  10. Python学习:运算符

    简单运算符: +(加) 两个对象相加 -(减) 从一个数中减去另一个数,如果第一个操作数不存在,则假定为零 *(乘) 给出两个数的乘积,或返回字符串重复指定次数后的结果   Eg.'haha' * 3 ...