蜗牛慢慢爬 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(最接近的三数之和)
随机推荐
- SDOI 2018 R2 游记
一篇真正的“游记”. DAY -3 下午: 今天老师批准了我去省选划水的请假要求. 批准之后感觉学习非常有动力,顺便忽悠别的同学和我一起去,然而wzx是唯一一个表示可以考虑一下的同学,其他同学直接一口 ...
- k8s mongodb 集群配置
service.yaml apiVersion: v1 kind: Service metadata: name: mongo labels: name: mongo spec: ports: - p ...
- Jenkins忘记密码解决办法
1.进入 如果安装的war包,路劲如下: C:\Users\LENOVO\.jenkins\ 2.打开config.xml ->将useSecurity设置为false 3.进入系统管理的管理 ...
- Node.js实战(十一)之Buffer
JavaScript 语言自身只有字符串数据类型,没有二进制数据类型. 但在处理像TCP流或文件流时,必须使用到二进制数据.因此在 Node.js中,定义了一个 Buffer 类,该类用来创建一个专门 ...
- pyspider爬取数据存入redis--1.安装驱动
首先安装pyredis的驱动 wget https://pypi.python.org/packages/source/r/redis/redis-2.9.1.tar.gz 解压并cd python ...
- js判断文本是否溢出容器
onShowNameTipsMouseenter: function(e) { var target = e.target; var containerLength = $(target).width ...
- day 26
今日内容 classmethod 让这个类中的方法绑定自己类,这样就可以直接用类调用该方法. staticmethod 让类中的方法编程非绑定方法,也就是是这个类中的方法编程普通函数. ####### ...
- float与double的范围和精度以及大小非零比较
1. 范围 float和double的范围是由指数的位数来决定的. float的指数位有8位,而double的指数位有11位,分布如下: float: 1bit(符号位) 8bits(指数位) ...
- 使用Novell.Directory.Ldap.NETStandard在.NET Core中验证AD域账号
Novell.Directory.Ldap.NETStandard是一个在.NET Core中,既支持Windows平台,又支持Linux平台,进行Windows AD域操作的Nuget包. 首先我们 ...
- Oracle-归档日志详解(运行模式、分类)
一.Oracle日志分类 分三大类: Alert log files--警报日志,Trace files--跟踪日志(用户和进程)和 redo log 重做日志(记录数据库的更改 ...