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. idea开启自动编译

    springboot+thymeleaf+idea   idea默认是不自动编译的. 首先热部署的jar包肯定是要加进去的 2,快捷键 ctrl+shift+alt+/       点击registr ...

  2. spring mvc 数据绑定入门

    1:基本数据类型key 是必传的并且必须类型一致 // http://localhost:8080/bind/baseType.do?xage=10 @RequestMapping(value = & ...

  3. c# 获取网络流量

    public class ip_helper{enum Constants {MAX_INTERFACE_NAME_LEN=256, MAXLEN_PHYSADDR=8,MAXLEN_IFDESCR= ...

  4. 蚯蚓(noip2016,贪心,单调性)

    题目描述 本题中,我们将用符号⌊c⌋ 表示对 c 向下取整,例如:⌊3.0⌋=⌊3.1⌋=⌊3.9⌋=3 . 蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭 ...

  5. ABAP术语-BOR (Business Object Repository )

    BOR (Business Object Repository ) 原文:http://www.cnblogs.com/qiangsheng/archive/2007/12/25/1013523.ht ...

  6. EF core Code First 简单的使用方法

    好吧,我又回来了,其实一直都想写一篇关于EF core 的文章去记录自己在开发时候遇到的问题. 为什么要使用EF框架呢,因为原始的ADO.NET需要编写大量的数据访问代码,所以使用EF会更方便.但是今 ...

  7. 构建高可靠hadoop集群之3- Quorum Journal Manager

    在正式环境中,搭建高可靠(ha)的系统是必须的. 例如oralce的rac,apache集群,windows服务器集群 本文不再赘言ha的重要性. 本文主要是对 http://hadoop.apach ...

  8. JSP/Servlet开发——第六章 JSP开发业务应用

    1. 大容量的数据显示的缺点: ●当数据量较多时,用户需要拖动页面才能浏览更多信息: ●数据定位不便: 2.分页显示: ●既能显示多条数据,又不需要拖动页面,是数据更加清晰直观,页面不再冗长,也不受数 ...

  9. vue入门——基本概念

    1. 挂载点,模板,实例的关系? 首先附上一个基本demo: <!DOCTYPE html> <html lang="en"> <head> & ...

  10. Hello,移动WEB—Viewport_Meta标签

    二   Viewport meta标签: 语法:<meta name="viewport" content="name=value, name=value" ...