思路

  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. git知识简单小结

    git特点: 1)分布式 2)存储快照而非差异 3)本地有完全的版本库,几乎所有操作都在本地 4)有内在的一致性,SHA1 5)优秀的分支管理 6)支持各种协同模式 7)开源,有一些第三方软件可整合使 ...

  2. 关于ETH/BTC区块的监控

    此次我写的是一个小型的shell, 链接钉钉的机器人, 使用过的应该会比较娴熟的了,下面就简述一下把 主要的功能就是, 当发现本地数据库区块跟网络上的区块差距相差较大的时候就代表, 数据同步有问题, ...

  3. 珠峰-node

    ##### 文件流的读写. ##### 文件流对pipe的封装. ####

  4. vue垂死挣扎--遇到的问题

    1, 原生js监听浏览器后退及禁用返回 +. 涉及到的history的知识 2, watch监听路由变化

  5. Python爬虫beautifulsoup4常用的解析方法总结(新手必看)

    今天小编就为大家分享一篇关于Python爬虫beautifulsoup4常用的解析方法总结,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧摘要 如何用beau ...

  6. PPT导出为图片

    使用Aspose组件导出 Aspose有Aspose.Slides.dll,可以无需安装office,进行读写PPT文件. Aspose可能通过Aspose.Slides.NET安装 简单的导出图片d ...

  7. spring cloud springboot 框架源码 activiti工作流 前后分离 集成代码生成器

    1.代码生成器: [正反双向](单表.主表.明细表.树形表,快速开发利器)freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本.处理类.service等完整模块2. ...

  8. Mysql数据库操作(命令行)

    1 环境 树莓派: mysql: 2  指令 以下是从命令行中连接mysql服务器的简单实例: [root@host]# mysql -u root -p Enter password:****** ...

  9. windows系统中如何删除文件路径过长的文件

    Q:通常在使用npm安装包后,路径会变的非常的长,有时候无法手动删除文件: 这个时候可以选中文件点击>添加文件到压缩文件,在弹出出来的框中勾选 压缩后删除原文件,>最后删除压缩包

  10. 数据库->神奇的concat_ws函数

    神奇的concat_ws函数 了解到concat_ws函数起因是因为:朋友给我发了一份面试题,其中有一句看似很简单的查询,但是我就是没做出来.如下图:   经查阅资料了解通过两个字段拼接并且用“_”进 ...