思路

  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. Vue中你可能认为是bug的情况原来是这样的

    前言 我们知道Vue框架剧本双向数据绑定功能,在我们使用方便的同时,还有一些细节问题我们并不知道,接下来一起探讨一些吧 双向数据绑定 js变量改变影响页面 页面改变影响js变量 Vue2是如何做到数据 ...

  2. Literature Review: Improving Image-Based Localization by Active Correspondence Search

    Abstract Input: A query image Source: A point cloud reconstruction of a large scene (有一百多万3D点) Resul ...

  3. WARNING: The host '$hostname' could not be looked up with resolveip. (转)

    环境介绍:CentOS6.X MySQL版本:5.5.X以上 执行scripts/mysql_install_db脚本时,抛出一条Warning,主机名和IP地址无法解析: The host '$ho ...

  4. ES6 - 基础学习(6): 对象扩展

    对象对于JavaScript至关重要,在ES6中对象又加了很多新特性. 对象字面量:属性的简洁表示法 ES6允许对象的属性直接写变量,这时候属性名是变量名,属性值是变量值. let attr1 = & ...

  5. 痞子衡嵌入式:ARM Cortex-M内核那些事(6)- 系统堆栈机制

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是ARM Cortex-M堆栈机制. 今天给大家分享的这篇依旧是2016年之前痞子衡写的技术文档,花了点时间重新编排了一下格式.前面痞子衡 ...

  6. eclipse中一个项目引用另一个项目的方法

    我们在开发的时候,有时候需要把一个大的项目打散,尤其是现在微服务的架构很流行,一个大的项目往往被拆成很多小的项目,而有的项目作为公共工程被独立出来,比如有个工程专门提供各种Util工具类,有的工程专门 ...

  7. day 16内置函数总结

    reversed()l = [1,2,3,4,5]l.reverse()print(l) l = [1,2,3,4,5]l2 = reversed(l)reversed:更加节省内存资源print(l ...

  8. 个性化和云端孤岛困扰SaaS用户,低代码PaaS或成解决之道 ZT

    近日,中国软件行业协会.中国软件网联合阿里云推出了<2020中国SaaS产业十大趋势>,其中明确指出企业软件SaaS化是大势所趋,但个性化和云端孤岛成为2020年SaaS用户关注的两大问题 ...

  9. 逻辑卷管理(LVM)-迁移

    逻辑卷管理(LVM)-迁移 更换卷组中逻辑卷中的一块硬盘流程:1确保卷组剩余空间大于需要更换的空间(缩减或添加添加新空间)-2迁移-3从卷组删除-4删除物理卷 #移除sdc1 1.查看卷组可用空间是否 ...

  10. ihandy2019笔记编程真题

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...