蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]
题目
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
翻译
和 3sum 那道题大同小异... 传送门
只不过这次不再是求和为0的三个数也不是返回所有解,而是求出离target最近的三个数的和并返回
Hints
Related Topics: Array, Two Pointers
代码
Java
class Solution {
public int threeSumClosest(int[] nums, int target) {
int result = nums[0]+nums[1]+nums[nums.length-1];
Arrays.sort(nums);
for(int i=0;i<nums.length-2;i++){
int l = i+1, r = nums.length-1;
while(l<r){
int s = nums[i]+nums[l]+nums[r];
if(s>target)
r--;
else
l++;
if(Math.abs(target-s)<Math.abs(target-result))
result = s;
}
}
return result;
}
}
Python
class Solution(object):
def threeSumClosest(self, nums, target):
nums.sort()
closest = nums[0]+nums[1]+nums[len(nums)-1]
if len(nums)<3: return
for i in range(0,len(nums)-2):
l, r = i+1, len(nums)-1
while l<r:
s = nums[i]+nums[l]+nums[r]
if abs(target-s)<=abs(target-closest):
closest = s
if s<target:
l+=1
elif s>target:
r-=1
else:
return target
return closest
蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]的更多相关文章
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 蜗牛慢慢爬 LeetCode 15. 3Sum [Difficulty: Medium]
题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]
题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...
- 蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]
题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
随机推荐
- BZOJ1121:[POI2008]激光发射器SZK(乱搞)
Description 多边形相邻边垂直,边长为整数,边平行坐标轴.要在多边形的点上放一些激光发射器和接收器.满足下列要求: 1发射器和接收器不能放置在同一点: 2发射器发出激光可以沿壁反射,最终到达 ...
- OpenCV——模板匹配
minMaxLoc函数: void minMaxLoc( const Mat& src, double* minVal, double* maxVal=0, Point* minLoc=0, ...
- JAVA框架Struts2(二)
一:Struts2执行流程: 1)编写页面,点击超链接,请求提交到服务器端. 2)请求先经过Struts2核心过滤器(StrutsprepareAndexectuterfilter). 3)过滤器的功 ...
- Linux安装配置apache
Linux安装配置apache 1.获取软件: http://httpd.apache.org/ httpd-2.2.21.tar.gz 2.安装步骤: 解压源文件: 1 tar zvxf ht ...
- jqgrid 谈谈给表格设置列头事件、行事件、内容事件
往往我们需要给显示的jqgrid表格赋予事件功能,比如:列头事件.行事件.内容事件.需要的效果可能如下: 如你所见,以上的超链接和按钮均是绑定的事件.那分别如何实现这些事件的绑定呢? 一.行事件 行事 ...
- LIN、CAN、FlexRay、MOST,三分钟搞明白四大汽车总线
LIN.CAN.FlexRay.MOST,三分钟搞明白四大汽车总线 2016-09-21 13:09 汽车中的电子部件越来越多,光是ECU就有几十个,这么多的电子单元都要进行信息交互.传统的点对点通信 ...
- 20155307《网络对抗》Web安全基础实践
20155307<网络对抗>Web安全基础实践 基础问题回答 SQL注入攻击原理,如何防御? 原理:SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL ...
- 20155310 Exp9 Web安全基础实践
20155310 Exp9 Web安全基础实践 基础问题 SQL注入攻击原理,如何防御? SQL注入漏洞是指在Web应用对后台数据库查询语句处理存在的安全漏洞.也就是,在输入字符串中嵌入SQL指令,在 ...
- HTML5 本地存储实现购物车功能
在家休陪产假,无聊,看自己以前的项目,突然发现之前写的购物车都是用数据库实现的,数据库实现购物车原则上没什么问题,但是需要和数据库交互,这样无意之间降低了程序的效率.今天突发奇想,如果能用HTML5本 ...
- Docker_容器化gitlab
Docker部署接口自动化持续集成环境第一步,容器化一个Gitlab! 1:开放防火墙端口 sudo yum install curl openssh-server openssh-clients p ...