思路

  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)

  1. class Solution {
  2. public int threeSumClosest(int[] nums,int target) {
  3. int sum = nums[0]+nums[1]+nums[2];
  4. int ans = sum;
  5. int minDiff = Math.abs(sum-target);
  6. Arrays.sort(nums);
  7. int len = nums.length;
  8. int l;
  9. int r;
  10. int diff;
  11. for (int i = 0; i+3 <= len; ++i) {
  12. if (nums[i]*3 >= target+minDiff)
  13. break;
  14. l = i+1; r = len-1;
  15. while (l<r) {
  16. sum = nums[i]+nums[l]+nums[r];
  17. diff = Math.abs(sum-target);
  18. if (diff < minDiff) {
  19. minDiff = diff;
  20. ans = sum;
  21. }
  22. if (sum > target)
  23. --r;
  24. else if (sum < target)
  25. ++l;
  26. else
  27. return target;
  28. }
  29. }
  30. return ans;
  31. }
  32. }

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. Linux 配置ip 子接口 多网卡绑定

    linux系统配置ip地址,图形化界面略过,这里只介绍文本行.做以下设置注意是否有此权限 查看当前路由及网关信息: [root@localhost ~]# netstat -r Kernel IP r ...

  2. ubuntu14.04安装mysql5.6.37

    摘抄这篇文档是为了记录自己的日常学习情况,方便以后查看.后边注明了来源,如有不对的地方,希望大家指正,谢谢! 首先从mysql官网上下载所需的离线包,我现在的版本是(mysql-5.6.37-linu ...

  3. el-menu 菜单展示

    <template> <div class="tab-container"> <el-menu class="el-menu-vertica ...

  4. Java第一次代码作业汇总

    练习题1:(完数问题) 求100以内的所有完数.(完数:它所有因子之和等于其本身)   方法一: /* 解体思路:1.先找出每个数的所有因子 2.比较因子之和是否与其数本身相等 */ public c ...

  5. Ream--(objc)写事务精简方案

    Ream--(objc)写事务精简方案 地址: REALM-- Realm官方提供的的写事务有两种方式: A[realm beginWriteTransaction]; // ... [realm c ...

  6. 【DTOJ】2703:两个数的余数和商

    DTOJ 2703:两个数的余数和商  解题报告 2017.11.10 第一版 ——由翱翔的逗比w原创,引用<C++ Primer Plus(第6版)中文版> 题目信息: 题目描述 给你a ...

  7. AD常用命令以及概念

    活动目录服务器常用命令合集如下: net accounts  查看第一台域控的计算机角色net accounts   查看计算机角色net share      查看共享netdom query fs ...

  8. opencv —— HoughCircles 霍夫圆变换原理及圆检测

    霍夫圆变换原理 霍夫圆变换的基本原理与霍夫线变换(https://www.cnblogs.com/bjxqmy/p/12331656.html)大体类似. 对直线来说,一条直线能由极径极角(r,θ)表 ...

  9. ext4文件系统启动自检的必要性

    最近我们发现多个用户设备掉电后重启,系统不工作. 研究这些返修设备,发现这些设备的表象是网络连接失败,DNS resolve不了.进一步发现/etc/resolv.conf为空,所以应用程序没法进行D ...

  10. Docker Compose 启动mysql,redis,rabbitmq

    这里使用的centos7,首先切换到root. sudo -s 首先去设置下载镜像,否则下载这三个东西要很久,而且可能失败. vim /etc/docker/daemon.json 内容如下: { & ...