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. (思维导图搞定)Content-Type:application/json,后台如何接收

    自己定的规范:只要Content-Type设置为application/json的时候,前台的data要传递字符串 虽然设置为application/json,前台传对象request.getPara ...

  2. rsync的daemon模式

    官方文档:https://download.samba.org/pub/rsync/rsyncd.conf.html   1:daemon模式配置文件         rsync以daemon方式运行 ...

  3. utf-8mb4和排序规则

    MySQL在5.5.3之后增加了这个utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode. 最新的 UTF-8 规范只使用一到四个字节,最大能编码21位, ...

  4. centos7如何使用yum命令

    参照https://www.cnblogs.com/zhongguiyao/p/9029922.html 参照https://blog.csdn.net/shuaigexiaobo/article/d ...

  5. Win7系统安装Centos7.0双系统(一)

    项目环境测试,过去在虚拟机安装只要配置好镜像很快就可以轻松安装,但是在真实机中安装就有些略坑.网上有很多说法是把iso文件中安装引导拷出来,试了很久还是觉得U盘镜像直接安装更便捷.靠谱.因为CentO ...

  6. Metasploit 简单渗透应用

    1.Metasploit端口扫描: 在终端输入msfconsole或直接从应用选metasploit 进入msf>nmap -v -sV 192.168.126.128  与nmap结果一样 用 ...

  7. 虚拟机克隆之后,网卡名称从eth0变成eth1之后的解决办法

    使用VMware安装了CentOS虚拟机,克隆之后使用service network restart指令来重新启动网络服务时,会看到有eth0网卡不存在的提示.出现这种现象的原因是,很多Linux d ...

  8. 学习 MeteoInfo二次开发教程(五)

    1.ActiveMapFrame部分没有大问题,按教程来就行. private void SetMapView()和private void ActiveMapFrameChanged(object ...

  9. Java分割ID和姓名(String不能当输出参数)

    ID:包括数字和字母 姓名:汉字 package org.ah; import org.ah.utils.Utils; public class Test { public static void m ...

  10. android 开发 View _6_Canvas详解

    牛逼大神的博客地址:http://www.gcssloop.com/customview/Canvas_BasicGraphics 安卓自定义View进阶-Canvas之绘制图形 在上一篇自定义Vie ...