LeetCode 3sum-closest 题解
思路
- 排序
- 枚举一个数a
- 双指针移动法确定b和c
- 求和,更新最接近的值
复杂度
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 题解的更多相关文章
- [LeetCode]3Sum Closest题解
3sum Closest: Given an array S of n integers, find three integers in S such that the sum is closest ...
- [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 ...
- 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 ...
- 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 ...
- LeetCode——3Sum Closest
Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- leetcode 3Sum Closest python
class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List ...
- LeetCode 3Sum Closest 最近似的3sum(2sum方法)
题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...
- 《LeetBook》leetcode题解(16):3Sum Closest [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 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 ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
随机推荐
- Linux 磁盘原理与管理 (分区 挂载)
目前硬盘与主机系统的联系主要有几种传输介面规格: IDE介面: IDE介面插槽所使用的排线较宽,每条排线上面可以接两个IDE装置,由於可以接两个装置,那为了判别两个装置的主/从架构, ...
- C#调用Windows API(示例:显示任务管理器里的程序名称)
作为初学者来说,在C#中使用API确是一件令人头疼的问题. 在使用API之前你必须知道如何在C#中使用结构.类型转换.安全/不安全代码,可控/不可控代码等许多知识. 在.Net Framework S ...
- vue-cli项目引入jquery和bootstrap
1.安装插件 npm install jquery --save npm install bootstrap --save npm install popper.js --save //提示框插件,b ...
- Lambda如何实现条件去重distinct List,如何实现条件分组groupBy List
条件去重 我们知道, Java8 lambda自带的去重为 distinct 方法, 但是只能过滤整体对象, 不能实现对象里的某个值进行判定去重, 比如: List<Integer> nu ...
- 进阶之路 | 奇妙的Animation之旅
前言 本文已经收录到我的Github个人博客,欢迎大佬们光临寒舍: 我的GIthub博客 学习清单: 动画的种类 自定义View动画 View动画的特殊使用场景 属性动画 使用动画的注意事项 一.为什 ...
- splice和slice这两兄弟为毛这么难记
容易混淆,决定做下笔记!!! splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目. 语法 arrayObject.splice(index,howmany,item1,....., ...
- Flink中逻辑计划和物理计划的概念划分和对应关系
逻辑计划 logicGraph或者jobGraph,其端点为operator,edge为数据流向. operator往往代表一个函数. 同一个分区内的具有连续上下游关系的函数组成operator-ch ...
- css的核心原理分为优先级原则与继承原则两大部分
css原理:1.优先原则=>后解析的内容会覆盖之前解析的内容(所谓解析就是读取的css样式)2.继承原则=>嵌套里面的标签拥有外部标签的某些样式,子元素可以继承父元素的属性 1>优先 ...
- html基本标签表单实现交互原理,单选框,复选框,下拉框介绍
表单是什么?表单是前端和服务器做交互的一种机制,表单收集用户输入信息,之后发送或者提交给服务器.用户在输入的信息称之为内容,内容的文本分为普通和密码型,用户通过单选框.复选框.下拉框(也就是下拉菜单) ...
- Html介绍,认识head标签
<head></head>标签位于html文档的头部,主要是用来描述文档的各种属性和信息,包括文档的标题等,当然文档头部包含的数据都不会真正作为内容展示给访客的. 如下的一些标 ...