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. 图片验证码自动识别,使用tess4j进行验证码自动识别(java实现)

    1.下载tess4j依赖的jar包,maven中央库地址:<dependency>            <groupId>net.sourceforge.tess4j< ...

  2. Webstrom 连接svn报错怎么解决

    Subversion: (Accessing URL:  https://192.168.1.249:8443/svn/H5/seif ) Received fatal alert: handshak ...

  3. 彻底解决tap“点透”,提升移动端点击响应速度

    申明!!!最后发现判断有误,各位读读就好,正在研究中.....尼玛水太深了 前言 近期使用tap事件为老夫带来了这样那样的问题,其中一个问题是解决了点透还需要将原来一个个click变为tap,这样的话 ...

  4. VC学习笔记: 1. Window程序内部运行机制

    0. 内容结构 API与SDK 窗口与句柄 消息与消息队列 WinMain函数 1. API与SDK 这里 API是指由Windows操作系统提供给应用程序的编程接口: Windows系统提供的API ...

  5. 索引图像(X与map)的显示、保存、转化

    有的图像载入后,出现X.map两个矩阵,那么他就是索引图像. load wbarb; figure,imshow(X,map);%显示原图 imwrite(X,map,'C:\Users\Jv\Des ...

  6. AOE 网络

    1.定义 如果在无向环的带权有向图中 - 用有向边表示一个工程中的活动 - 用边上的权值表示活动的持续时间 - 用顶点表示事件 则这样的有向图叫做用边表示活动的网络,简称AOE网络 AOE在工程方面非 ...

  7. B树(B-树)

    1.什么是B树(B-树)? B树是一种m阶树,m>=2 性质: 1)树中每个结点至多m个孩子: 2)对于根结点,子树个树取值范围为[2,m],关键字个数范围[1,m-1]: 3)对于非根非叶结点 ...

  8. git clone 出现 RPC failed 错误的解决方案

    今天使用git clone一个大型项目的时候出现了如下错误:

  9. Vultr VPS测试IP $5/月KVM-512MB/15G SSD/1T

    vultr主要卖点是:SSD硬盘.超低价格.全球13个机房.10Gb服务器带宽.3GHz CPU.性能优异.vultr vps详细评测可参考这篇文章. 以下是vutlr vps官方提供的测试IP地址: ...

  10. digitalocean vpn安装配置教程

    digitalocean是美国一家专业的vps提供商,优势是性价比高,最低配置512MB内存vps每月只要5美元,导致大陆用户疯狂涌入.关于digitalocean申请方法.digitalocean速 ...