[LeetCode] 16. 最接近的三数之和
题目链接:https://leetcode-cn.com/problems/3sum-closest/
题目描述:
给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。
示例:
例如,给定数组 nums = [-1,2,1,-4], 和 target = 1.
与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).
思路:
一句话解释:固定一个值,找另外两个值(双指针).
数组是已排好序,首先确定一个数,在左右指针运动过程中,记录与target绝对值差值最小的.
可以关注我的知乎专栏,了解更多解题方法!
代码:
python
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
nums.sort()
#print(nums)
n = len(nums)
res = float("inf")
for i in range(n):
if i > 0 and nums[i] == nums[i-1]:
continue
left = i + 1
right = n - 1
while left < right :
#print(left,right)
cur = nums[i] + nums[left] + nums[right]
if cur == target:return target
if abs(res-target) > abs(cur-target):
res = cur
if cur > target:
right -= 1
elif cur < target:
left += 1
return res
java
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
//int res = Integer.MAX_VALUE;
int n = nums.length;
int res = nums[0] + nums[1] + nums[n-1];
for (int i = 0; i < n - 2; i++) {
if (i > 0 && nums[i] == nums[i-1]) continue;
int left = i + 1;
int right = n - 1;
while (left < right) {
int cur = nums[i] + nums[left] + nums[right];
if (cur == target) return target;
if (Math.abs(res - target) > Math.abs(cur - target)) res = cur;
if (cur > target) right -= 1;
if (cur < target) left += 1;
}
}
return res;
}
}
c++
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int n = nums.size();
// c++ 排序
sort(nums.begin(),nums.end());
int res = nums[0] + nums[1] + nums[2];
for (int i = 0; i < n - 2; i++){
if (i > 0 && nums[i] == nums[i-1]) continue;
int left = i + 1;
int right = n - 1;
while (left < right){
int cur = nums[i] + nums[left] + nums[right];
if (cur == target) return target;
if (abs(res - target) > abs(cur - target)) res = cur;
if (cur > target) right -= 1;
if (cur < target) left += 1;
}
}
return res;
}
};
[LeetCode] 16. 最接近的三数之和的更多相关文章
- Java实现 LeetCode 16 最接近的三数之和
16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...
- LeetCode 16. 最接近的三数之和(3Sum Closest)
题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例 ...
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
- Leetcode题库——16.最接近的三数之和
@author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...
- leetcode.数组.16最接近的三数之和-java
1. 具体题目 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案 ...
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- 【LeetCode】最接近的三数之和【排序,固定k1,二分寻找k2和k3】
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- LeetCode-016-最接近的三数之和
最接近的三数之和 题目描述:给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只 ...
随机推荐
- MySQL执行原理,逻辑分层、更改数据库处理引擎
MySQL执行原理,逻辑分层.更改数据库处理引擎 作者:Stanley 罗昊 [转载请注明出处和署名,谢谢!] 用了那么长时间的MySQL,sql语句相信早已烂熟于心,于是,我就试着去了解它的执行原理 ...
- 【SpringCloud Eureka源码】从Eureka Client发起注册请求到Eureka Server处理的整个服务注册过程(下)
目录 一.Spring Cloud Eureka Server自动配置及初始化 @EnableEurekaServer EurekaServerAutoConfiguration - 注册服务自动配置 ...
- 利用BGP虚拟下一跳实现链路负载均衡
最近针对BGP链路负载均衡方案“虚拟下一跳”进行了总结,现将总结的PPT贴上来.
- tomcat 大并发报错 Maximum number of threads (200) created for connector with address null and port 80
1.INFO: Maximum number of threads (200) created for connector with address null and port 80 说明:最大线程数 ...
- [争什么! 掺在一起做撒尿牛丸啊! 笨蛋]ASP.NET Core 2.0 + EF6 + Linux +MySql混搭
好消息!特好消息!同时使用ASP.NET Core 2.0和.NET Framework类库还能运行在linux上的方法来啦! 是的,你没有看错!ASP.NET Core 2.0,.NET Frame ...
- C# 处理Excel公式(一)——创建、读取Excel公式
对于数据量较大的表格,需要计算一些特殊数值时,我们通过运用公式能有效提高我们数据处理的速度和效率,对于后期数据的增删改查等的批量操作也很方便.此外,对于某些数值的信息来源,我们也可以通过读取数据中包含 ...
- JavaScript是如何工作的:使用MutationObserver跟踪DOM的变化
摘要: 掌握MutationObserver. 这是专门探索 JavaScript 及其所构建的组件的系列文章的第10篇. 如果你错过了前面的章节,可以在这里找到它们: JavaScript 是如何工 ...
- 细说addEventListener与事件捕获
细说addEventListener与事件捕获.事件冒泡(一)addEventListener的基本用法 在复杂的项目开发中,javascript和html的解耦变得至关重要,我们被推荐使用事件动态绑 ...
- 用 async/await 来处理异步
昨天看了一篇vue的教程,作者用async/ await来发送异步请求,从服务端获取数据,代码很简洁,同时async/await 已经被标准化,是时候学习一下了. 先说一下async的用法,它作为一个 ...
- linux c ---raise 使用范例的代码
把做工程过程中比较好的代码片段收藏起来,下面代码内容是关于linux c ---raise 使用范例的代码,希望对各位有所用途. #include <sys/types.h> #inclu ...