思路

  1. 排序
  2. 枚举一个数a
  3. 双指针移动法确定b和c
  4. 求和,更新最接近的值

复杂度

T(n)=O(n2)  M(n)=O(1)T(n)=O(n^2) \; M(n)=O(1)T(n)=O(n2)M(n)=O(1)

class Solution {
public int threeSumClosest(int[] nums,int target) {
int sum = nums[0]+nums[1]+nums[2];
int ans = sum;
int minDiff = Math.abs(sum-target);
Arrays.sort(nums);
int len = nums.length;
int l;
int r;
int diff;
for (int i = 0; i+3 <= len; ++i) {
if (nums[i]*3 >= target+minDiff)
break;
l = i+1; r = len-1;
while (l<r) {
sum = nums[i]+nums[l]+nums[r];
diff = Math.abs(sum-target);
if (diff < minDiff) {
minDiff = diff;
ans = sum;
}
if (sum > target)
--r;
else if (sum < target)
++l;
else
return target;
}
}
return ans;
}
}

Runtime: 10 ms, faster than 85.33% of Java online submissions for 3Sum Closest.

Memory Usage: 37.8 MB, less than 100.00% of Java online submissions for 3Sum Closest.

LeetCode 3sum-closest 题解的更多相关文章

  1. [LeetCode]3Sum Closest题解

    3sum Closest: Given an array S of n integers, find three integers in S such that the sum is closest ...

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

  3. 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 ...

  4. LeetCode 3Sum Closest (Two pointers)

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

  5. LeetCode——3Sum Closest

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

  6. leetcode 3Sum Closest python

    class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List ...

  7. LeetCode 3Sum Closest 最近似的3sum(2sum方法)

    题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...

  8. 《LeetBook》leetcode题解(16):3Sum Closest [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum

    1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...

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

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

随机推荐

  1. java String hashCode遇到的坑

    在进行数据交换时,如果主键不是整型,需要对字符串,或联合主键拼接为字符串,进行hash,再进行取模分片,使用的是String自带的hashCode()方法,本来是件很方便的事,但是有些字符串取hash ...

  2. h5笔记1

    1.HTML中不支持 空格.回车.制表符,它们都会被解析成一个空白字符 2.适用于大多数 HTML 元素的属性: class 为html元素定义一个或多个类名(classname)(类名从样式文件引入 ...

  3. console控制台的用法

    参考链接:https://developer.mozilla.org/zh-CN/docs/Web/API/Console 1,console.log('a'); 2,console.dir(xx); ...

  4. iis添加asp.net网站,访问提示:由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射

    今天在iis服务器配置asp.net网站,遇到一个问题,记录一下: 问题:由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. Windo ...

  5. java开发病房管理系统

    开发环境: Windows操作系统开发工具: Myeclipse+Jdk+Tomcat+MySQL数据库 运行效果图 源码及原文链接:https://javadao.xyz/forum.php?mod ...

  6. 基于SSM开发在线家教预约系统源码

    开发环境: Windows操作系统开发工具:Eclipse+Jdk+Tomcat8+mysql数据库 注意:次项目运行Tomcat8服务器里面 次项目比较大,需要自行研究 运行效果图 源码及原文链接: ...

  7. Programming on C 学习笔记

    目录 include不同的声明方式有什么不同? if defined 与 #ifdef 有什么区别? undef 是怎么工作的? 如何利用 typedef 来定义数组? 枚举中,如果有个元素被赋予值, ...

  8. Django2.2 静态文件的上传显示,遇到的坑点-------已解决

    前情提要:这里虽说是Django2.2 ,但经过测试发现Django 的其他版本也可以用此方法解决 一.项目根目录下的static文件的路由显示问题 在项目根目录下创建静态文件时发现,即使我配置了se ...

  9. PostgreSQL内核学习笔记十一(索引)

    Index Scan涉及到两部分的内容Heap Only Tuple和index-only-scan. 什么是Heap Only Tuple(HOT)? 例如:Update a Row Without ...

  10. 【MVC+EasyUI实例】对数据网格的增删改查(下)

    前言 继上文对网格加载数据,本文主要阐述对数据增删改的实现. 一.js代码 function Add() { $("#dlg").dialog('open'); $("# ...