problem: given an array of integers including positive and negative, a target value. find 2 numbers in the array such that the sum of the 2 numbers is the closest to the target value.

Solution: this can be solved in O(nlogn).

First, invest some time to sort the array, O(nlogn).

Then,

int min= Integer.MAX;
while(i< j){
if(A[i]+ A[j]== T) return (i,j);
(A[i]+ A[j]- T> 0)? j--: i++;
}

find the closest sum to a target value的更多相关文章

  1. LeetCode 16 3Sum Closest (最接近target的3个数之和)

    题目链接 https://leetcode.com/problems/3sum-closest/?tab=Description     Problem : 找到给定数组中a+b+c 最接近targe ...

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

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

  3. (hash map)Two Sum, sorted(排序+双指针)closest,小于或大于的对数,组成不同的对数

    原版 sorted [抄题]: [思维问题]: 存sum - nums[i](补集),若出现第二次则调出 [一句话思路]: hashmap中,重要的数值当做key,角标当做value. [画图]: [ ...

  4. 查找表,Two Sum,15. 3Sum,18. 4Sum,16 3Sum Closest,149 Max points on line

    Two Sum: 解法一:排序后使用双索引对撞:O(nlogn)+O(n) = O(nlogn) , 但是返回的是排序前的指针. 解法二:查找表.将所有元素放入查找表, 之后对于每一个元素a,查找 t ...

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

  6. No.016:3Sum Closest

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

  7. No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

  8. C#解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 ...

  9. LeetCode第[16]题(Java):3Sum Closest 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...

随机推荐

  1. ubuntu 文件编码错误

    linux 打开出现中文编码错误(invalid encoding): convmv -f gbk -t utf-8 -r --notest /filename

  2. B/S、C/S区别

    [B/S.C/S C/S (Client/Server客户端服务器) B/S (Brower/Server浏览器服务器)  区别 1.硬件环境不同: C/S 一般建立在专用的网络上, 小范围里的网络环 ...

  3. .net core 单元测试小记

    创建项目,选择.net core的类库(class library) 修改project.json的配置 { "version": "1.0.0-*", &qu ...

  4. 给图片使用border-radius 图片会变成圆的。

  5. linux中找不到/etc/sysconfig/iptables

    解决办法初始化iptables. #iptables -F #service iptables save #service iptables restart

  6. 淘淘商城_day03_课堂笔记

    今日大纲 实现商品的编辑 实现商品的规格参数功能 搭建前台系统 实现首页商品类目的显示 商品的编辑 数据的编辑核心是:数据回显. 编辑按钮事件 判断选中的行数 弹出window 加载编辑页面,在页面加 ...

  7. javascript 延时执行函数

    延时执行函数,貌似有些多此一举, 也许还是有点用 记在这儿 var test = { delay : function(lifetime){ var data; setTimeout(function ...

  8. 计算机网络课程优秀备考PPT之第四章介质访问控制层(四)

    为了记录自己从2016.9~2017.1的<计算机网络>助教生涯,也为了及时梳理和整写笔记! 前期博客是, 计算机网络课程优秀备考PPT之第一章概述(一) 计算机网络课程优秀备考PPT之第 ...

  9. Mac下svn的使用

    1.从本地导入代码到服务器(第一次初始化导入) 在终端中输入 svn import /Users/apple/Documents/eclipse_workspace/weibo svn://local ...

  10. aps.net 页面事件执行顺序