Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:

Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

题意:

给定一个数组和一个值target,找出其中3个元素,使其加起来最接近target。

Solution1:Two Pointers(left and right to meet each other)

1. Sort the array (coz we must check the duplicates)

2. Lock one pointer and do two sum with the other two

3. Keep updating the gap between given target with result. The smaller gap is, the closer result is.

code

 /*
Time Complexity: O(n^2). For each locked item, we need traverse the rest of array behind such item.
Space Complexity: O(1). We only used constant extra space.
*/
class Solution {
public int threeSumClosest(int[] nums, int target) {
int result = 0;
int minGap = Integer.MAX_VALUE;
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
int j = i + 1;
int k = nums.length - 1;
while (j < k) {
int sum = nums[i] + nums[j] + nums[k];
int gap = Math.abs(target - sum);
// keep updating the gap. The smaller gap is, the closer result is.
if (gap < minGap) {
result = sum;
minGap = gap;
}
// two pointers(left and right to meet each other)
if (sum < target) {
j++;
} else {
k--;
}
}
}
return result;
}
}

[leetcode]16. 3Sum Closest最接近的三数之和的更多相关文章

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

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

  2. 【LeetCode】16. 3Sum Closest 最接近的三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...

  3. Leetcode16.3Sum Closest最接近的三数之和

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

  4. 016 3Sum Closest 最接近的三数之和

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

  5. LeetCode(16):最接近的三数之和

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

  6. C#LeetCode刷题之#16-最接近的三数之和(3Sum Closest)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3620 访问. 给定一个包括 n 个整数的 ...

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

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

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

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

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

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

随机推荐

  1. 开发一个简单的postgresql extension

      主要是学习如何编写一个简单的pg extension,参考https://severalnines.com/blog/creating-new-modules-using-postgresql-c ...

  2. ASP.NET资源大全-知识分享 【转载】

    API 框架 NancyFx:轻量.用于构建 HTTP 基础服务的非正式(low-ceremony)框架,基于.Net 及 Mono 平台.官网 ASP.NET WebAPI:快捷创建 HTTP 服务 ...

  3. py-day3-4 python 匿名函数

    # 匿名函数 lamdba name = 'xiaoma' f = lambda x:x+'jun' res = f(name) print('匿名函数的运行结果:',res) 匿名函数的运行结果: ...

  4. 全面理解Java内存模型(JMM)及volatile关键字

    [版权申明]未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) http://blog.csdn.net/javazejian/article/details/72772461 出自[zejian ...

  5. FPGA 中三角函数的实现

    FPGA 中三角函数的实现

  6. ubuntu-docker入门到放弃(七)Dockerfile简介

    一.dockerfile基本结构 最简单的理解就是dockerfile实际上是一些命令的堆叠,有点像最基础的shell脚本,没有if 没有for,就是串行的一堆命令. 一般而言,dockerfile分 ...

  7. windows copy 和xcopy

    #将文件夹下文件拷贝到指定目录 将d盘下1文件夹内所有内容拷贝到测试目录下copy d:\1\ Z:\chenkai\测试目录\ /yxcopy D:\soft\svn工具 Z:\chenkai\测试 ...

  8. Lepus监控之Oracle配置

    1.安装cx_Oracle a.官网下载客户端组件包 oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpmoracle-instantclien ...

  9. Chapter 5 数组:为什么很多编程语言种数组都是从0开始编号?

    如何实现随机访问? 线性表:数组,队列,链表,栈 非线性表:树,图 总结:数组用一块连续的内存空间,来存储相同类型的一组数据,最大的特点就是支持随机访问,但插入,删除操作也因此变得比较低效,平均情况时 ...

  10. 自动配置pom文件,构建maven项目jar包依赖关系,找到jar包运用到jmeter

    首先说下pom文件特别方便的优点: 什么是pom文件? POM(Project Object Model) 是Maven的基础. 它是一个XML文件,包含了Maven用来build项目所需要的项目配置 ...