[Leetcode] 3sum-closest 给定值,最为相近的3数之和
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).
题意:给定值target,在数列中找到三个数,它们的和最接近这个target。
思路:类似于Two sum、3sum还有本题,都可以先排序后,用夹逼法则:使用两个指针,从前向后,从后向前的遍历,找到符合条件的情况。为什么要使用这种方法了?针对本题,若是固定一个,然后再固定一个,通过移动最后一个指针,找到最小的差,然后,在重新将第二个指针移动一个位置,低三个指针,再重新遍历,这样耗时严重。利用好,已将数组排序这一条件,固定一个数,剩下的两个数分别从头和尾向中间遍历,若是三者的和大于target,则尾指针向左移动,减小对应的值,否则前指针右移增大对应的值,从而增大三者和。与此同时,更新和target最小的差和对应的sum。代码如下:
class Solution {
public:
int threeSumClosest(vector<int> &num, int target)
{
int sum=num[]+num[]+num[];
int diff=abs(sum-target);
sort(num.begin(),num.end());
for(int i=;i<num.size();++i)
{
int l=i+,r=num.size()-;
while(l<r)
{
int temp=num[i]+num[l]+num[r];
int newDiff=abs(temp-target);
if(diff>newDiff)
{
diff=newDiff;
sum=temp;
}
if(temp>target)
r--;
else
l++;
}
}
return sum;
}
};
[Leetcode] 3sum-closest 给定值,最为相近的3数之和的更多相关文章
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [LeetCode]3Sum Closest题解
3sum Closest: Given an array S of n integers, find three integers in S such that the sum is closest ...
- LeetCode(16):最接近的三数之和
Medium! 题目描述: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只 ...
- [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- #leetcode刷题之路16-最接近的三数之和
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- Leetcode 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- LeetCode 3Sum Closest (Two pointers)
题意 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- LeetCode 16 3Sum Closest (最接近target的3个数之和)
题目链接 https://leetcode.com/problems/3sum-closest/?tab=Description Problem : 找到给定数组中a+b+c 最接近targe ...
随机推荐
- Win7下如何安装python pygame的whl包
看了小甲鱼的python教程,对那个python版本的打飞机游戏很感兴趣,尝试运行,居然报错了,提示缺少pygame包: 仔细一看需要安装一个pygame的包,默认安装好python是不包括这个包的, ...
- JNI模板
java为了调用底层驱动函数,需要调用外部的C/C++代码,java提供了JNI接口: 然后将C代码编译成库(windows下 .dll / android环境下 .so) arm-linux-gcc ...
- 332. Reconstruct Itinerary
class Solution { public: vector<string> path; unordered_map<string, multiset<string>& ...
- windows 系统禁止使用 U 盘的方法
windows 系统禁止使用 U 盘的方法 最简单的办法: 注册表 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentCntrolSet\Services\USBSTOR] 将名为 ...
- laravel读excel
fileName = "test.xls";$filePath = "../storage/app/";Excel::load($filePath.$fileN ...
- Flexbox布局模式的理解
个人博客地址: 雨中的鱼-前端知识分享 http://www.showhtml5.cc 分享干货,有兴趣的人可以一起来分享前端知识 加Q群:440279380 Flexbox,一种C ...
- vs调试代码的时候断点无法命中
https://blog.csdn.net/xxdddail/article/details/18696399 该链接提供的解决方案主要是如下图片:禁用 图片标记的这个选项即可:
- Anytime项目开发记录0
Anytime,中文名:我很忙. 开发者:孤独的猫咪神. 这个项目会持续更新,直到我决定不再维护这个APP. 2014年3月10日:近日有事,暂时断更.希望可以会尽快完事. 2014年3月27日:很抱 ...
- Qt 建立带有子项目的工程
刚需,软件需要用到多个子项目 第一步 打开Qt新建子项目工程 如图 在此时鼠标右键,选着新建子项目如图 就是正常的新建项目的步骤,直接上图 完工,可以愉快的撸代码了
- 第十一篇 Python函数之定义&形参&实参&位置参数&关键字参数&可变长参数&默认参数
函数的定义:函数是为了完成某一特定功能的,函数是逻辑结构化和过程化的一种编程方法 函数的定义格式,函数一般都是有返回值的 #语法 #函数名要能反映其意义 def 函数名(参数1,参数2,参数3,... ...