Leetcode 16. 3Sum Closest(指针搜索)
131696FavoriteShare
Given an array nums
of n integers and an integer target
, find three integers in nums
such that the sum is closest to target
. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example:
Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). 【题解】:这个题我是真的卡了好几天,主要是因为上一道题没用用指针的方式搜索也过了。。。可以去看我上一道题的题解,这个题,很关键的理解点是,要把数据进行排序,然后再固定一个数,另外两个数就可以通过指针的方式搜索了
这道题让我们求最接近给定值的三数之和,是在之前那道 3Sum 的基础上又增加了些许难度,那么这道题让我们返回这个最接近于给定值的值,即我们要保证当前三数和跟给定值之间的差的绝对值最小,所以我们需要定义一个变量 diff 用来记录差的绝对值,然后我们还是要先将数组排个序,然后开始遍历数组,思路跟那道三数之和很相似,都是先确定一个数,然后用两个指针 left 和 right 来滑动寻找另外两个数,每确定两个数,我们求出此三数之和,然后算和给定值的差的绝对值存在 newDiff 中,然后和 diff 比较并更新 diff 和结果 closest 即可,代码如下:
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int diff = numeric_limits<int>::max();
int Final;
sort(nums.begin(),nums.end());
for(int i = ; i < nums.size()-; i++){
int tm1 = nums[i];
int left = i+; int right = nums.size()-;
while(left<right){
int sum = tm1+nums[left]+nums[right];
if(abs(target-sum) < diff){
diff = abs(target-sum) ;
Final = sum;
}
if(target==sum) return target;
else if(target < sum) right--;
else left++;
}
}
return Final;
}
};
Leetcode 16. 3Sum Closest(指针搜索)的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- [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] 16. 3Sum Closest ☆☆☆
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- Array + two points leetcode.16 - 3Sum Closest
题面 Given an array nums of n integers and an integer target, find three integers in nums such that th ...
- Leetcode 16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- Java [leetcode 16] 3Sum Closest
题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...
- [LeetCode] 16. 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 16. 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——16. 3Sum Closest
一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...
随机推荐
- IDEA激活—免费永久激活(lookdiv.com)
网址: http://lookdiv.com/ 钥匙就是网址 钥匙:lookdiv.com 亲测有效!非常好用.
- 【Linux开发】Ubuntu图形界面切换与磁盘扩展分区
Ubuntu14.04设置字符界面快捷键:ctrl-alt-f1 切换回图形界面:ctrl-alt-f7 为虚拟机拓展了30G的空间,挂在了/mnt/sda3这个目录下: 说明一下Ubuntu14.0 ...
- Java——ArrayList源码解析
以下针对JDK 1.8版本中的ArrayList进行分析. 概述 ArrayList基于List接口实现的大小可变的数组.其实现了所有可选的List操作,并且元素允许为任意类型,包括null元 ...
- 深入理解java:1.1. 类加载器
从java的动态性到类加载机制 我们知道,Java是一种动态语言. 那么怎样理解这个“动态”呢? 或者说一门语言具备了什么特性,才能称之为动态语言呢? 对于java,我是这样理解的. 我们都知道J ...
- Spring中的常见注解
@Controller 标识一个该类是Spring MVC controller处理器,用来创建处理http请求的对象. @RestController Spring4之后加入的注解,原来在@Co ...
- typescript是否可以直接编译执行?
算是个有趣的小问题,由于必须依赖node.js,typescript理论上是不能不转成js直接运行的.
- 关于微信H5页面开发中音乐不自动播放的解决方法
我想应该有很多人在做H5场景应用.H5微刊.H5微杂志的时候加入背景音乐吧(客户需求),相信很多人一定碰过不能自动播放的时候,即使是相同的iPhone 5s也有不播放的时候,很蛋疼吧!? 之前我的解决 ...
- python文件打包/导入 .so 文件
打包so文件 见: http://www.cnblogs.com/ke10/p/py2so.html 导入so文件 import sys sys.path.append(r'/home/project ...
- GUI自动化测试中优化测试用例思维方法
1.测试脚本与数据解耦(数据驱动) 让操作相同但是数据不同的测试可以通过同一 套自动化测试脚本来实现,只是在每次测试执行时提供不同的测试输入数据. 2.页面对象模型(POM) 以页面为单位来封装页面上 ...
- Python 入门之 推导式
Python 入门之 推导式 推导式就是构建比较有规律的列表,生成器,字典等一种简便的方式 1.推导式 (1)列表推导式 : <1> 普通循环: [变量 for循环] print([i f ...