蜗牛慢慢爬 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(最接近的三数之和)
随机推荐
- php实现链表的基本操作
<?php class node{ private $value; private $next; public function __construct($value=0,$next=null) ...
- trycatche
<?phpheader("Content-type: text/html; charset=utf-8"); try{$aaa = 0;if ($aaa == 9) {ech ...
- ElasticSearch + Logstash + Kibana 搭建笔记
ElasticSearch 安装 1.下载 ElasticSearch,本文使用的版本为 5.5.1. 2.配置 path.data: /data/es #数据路径 path.logs: /data/ ...
- 基于jmxtrans+influxdb+grafana实现对canal监控
最近在调研canal数据同步系统的监控方案,网上关于jmxtrans+influxdb+grafana监控kafka的文档很多,没有监控canal的.通过几天的摸索,大致明白了来龙去脉.监控流程基本是 ...
- pyspider爬取数据存入redis--1.安装驱动
首先安装pyredis的驱动 wget https://pypi.python.org/packages/source/r/redis/redis-2.9.1.tar.gz 解压并cd python ...
- OpenCV——SIFT特征检测与匹配
SIFT特征和SURF特征比较 比较项目 SIFT SURF 尺度空间极值检测 使用高斯滤波器,根据不同尺度的高斯差(DOG)图像寻找局部极值 使用方形滤波器,利用海森矩阵的行列式值检测极值,并利用积 ...
- Kafka设计解析(十九)Kafka consumer group位移重设
转载自 huxihx,原文链接 Kafka consumer group位移重设 本文阐述如何使用Kafka自带的kafka-consumer-groups.sh脚本随意设置消费者组(consumer ...
- jmeter.bat配置(主要关于OOM)
jmeter.bat是jmeter在windows系统下的启动文件.在使用jmeter压测,线程数设置过多时可能会报内存溢出(Out Of Memory Error),这时候可以去尝试调整一下jmet ...
- Linux 内核开发 - 内核定时器
时间差的度量 系统的定时器硬件以固定的频率产生时钟中断,产生始终中断的间隔以HZ 常量来决定,通常在50~1200之间,x86默认是1000.HZ能够依据不同的内核来配置. Linux 採用jiffi ...
- Flutter - JSON to Dart,一个json转dart实体的网站
如你所见,一个json转dart实体的网站,https://javiercbk.github.io/json_to_dart/