当前位置:网站首页>【LeetCode】16、最接近的三数之和
【LeetCode】16、最接近的三数之和
2022-07-01 14:53:00 【小曲同学呀】
16、最接近的三数之和
题目:
给你一个长度为 n 的整数数组 nums 和 一个目标值 target。请你从 nums 中选出三个整数,使它们的和与 target 最接近。
返回这三个数的和。
假定每组输入只存在恰好一个解。
示例 1:
输入:nums = [-1,2,1,-4], target = 1
输出:2
解释:与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。
示例 2:
输入:nums = [0,0,0], target = 1
输出:0
提示:
3 <= nums.length <= 1000
-1000 <= nums[i] <= 1000
-10 ^4 <= target <= 10 ^4
解题思路:
这个题目和15、三数之和,非常相似,可以同理使用双指针的思想。
首先,题目中所说最接近target的值,最接近也就是说三数之和的绝对值最接近目标值。
我们可以考虑直接使用三重循环枚举三元组,找出与目标值最接近的作为答案,时间复杂度为 O(N^3)。然而本题的 N 最大为 1000,会超出时间限制。所以不可取。
因此,正确的思路是:排序+双指针
- 标签:排序和双指针
- 本题目因为要计算三个数,如果靠暴力枚举的话时间复杂度会到 O(n^3),需要降低时间复杂度
- 首先进行数组排序,时间复杂度 O(nlogn)
- 在数组 nums 中,进行遍历,每遍历一个值利用其下标i,形成一个固定值 nums[i]
- 再使用前指针指向 start = i + 1 处,后指针指向 end = nums.length - 1 处,也就是结尾处
- 根据 sum = nums[i] + nums[start] + nums[end] 的结果,判断 sum 与目标 target的距离,如果更近则更新结果 ans
- 同时判断 sum 与 target 的大小关系,因为数组有序,如果 sum > target 则 end–,如果 sum <
target 则 start++,如果 sum == target 则说明距离为 0 直接返回结果 - 整个遍历过程,固定值为 n 次,双指针为 n 次,时间复杂度为 O(n^2)
- 总时间复杂度:O(nlogn) + O(n^ 2) = O( n^2)
参考代码:
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int ans = nums[0] + nums[1] + nums[2];
for(int i=0;i<nums.length;i++) {
int start = i+1, end = nums.length - 1;
while(start < end) {
int sum = nums[start] + nums[end] + nums[i];
if(Math.abs(target - sum) < Math.abs(target - ans))
ans = sum;
if(sum > target)
end--;
else if(sum < target)
start++;
else
return ans;
}
}
return ans;
}
}

边栏推荐
- 问题随记 —— Oracle 11g 卸载
- About the use of HTTP cache validation last modified and Etag
- C#学习笔记(5)类和继承
- MongoDB第二話 -- MongoDB高可用集群實現
- 这3款在线PS工具,得试试
- IDEA全局搜索快捷键(ctrl+shift+F)失效修复
- 30 Devops interview questions and answers
- 竣达技术丨室内空气环境监测终端 pm2.5、温湿度TVOC等多参数监测
- Redis安装及Ubuntu 14.04下搭建ssdb主从环境
- 2022-2-15 learning xiangniuke project - Section 1 filtering sensitive words
猜你喜欢

What problems should be considered for outdoor LED display?

2022-2-15 learning xiangniuke project - Section 1 filtering sensitive words

Salesforce, Johns Hopkins, Columbia | progen2: exploring the boundaries of protein language models

数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体

互联网医院系统源码 医院小程序源码 智慧医院源码 在线问诊系统源码

JVM performance tuning and practical basic theory part II

111. Minimum depth of binary tree

Microservice development steps (Nacos)

官宣:Apache Doris 顺利毕业,成为 ASF 顶级项目!

Minimum spanning tree and bipartite graph in graph theory (acwing template)
随机推荐
Ensure production safety! Guangzhou requires hazardous chemical enterprises to "not produce in an unsafe way, and keep constant communication"
使用net core 6 c# 的 NPOI 包,讀取excel..xlsx單元格內的圖片,並存儲到指定服務器
JVM performance tuning and practical basic theory part II
Fundamentals of C language
Build your own website (14)
[getting started with Django] 13 page Association MySQL "multi" field table (check)
购物商城6.27待完成
DirectX repair tool v4.1 public beta! [easy to understand]
It's suitable for people who don't have eloquence. The benefits of joining the China Video partner program are really delicious. One video gets 3 benefits
数字化转型:数据可视化赋能销售管理
Research Report on the development trend and competitive strategy of the global diamond suspension industry
TypeScript: let
JVM second conversation -- JVM memory model and garbage collection
241. Design priorities for operational expressions
Research Report on the development trend and competitive strategy of the global display filter industry
Blog recommendation | in depth study of message segmentation in pulsar
Cannot link redis when redis is enabled
Research Report on development trend and competitive strategy of global vibration polishing machine industry
qt捕获界面为图片或label显示
Reorganize the trivial knowledge points at the end of the term