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个数来,使得这三个数的 ...
随机推荐
- 【Linux开发】【Qt开发】arm-linux-gnueabihf-gdb versus gdb-multiarch
主要是说,在Ubuntu14.04 64bit的操作系统上,配置Qt的gdb和gcc的时候,在Qt build&run选项中,debugger中选中arm-linux-gnuabihf-gdb ...
- kafka学习(三)
kafka 消费者-从kafka读取数据 消费者和消费者群里 kafka消费者从属于消费者群组.一个群组里的消费者订阅的是同一主题,每个消费者接受主题一部分分区的消息.如果我们往群组里添加更多的消 ...
- java程序启动脚本
#!/bin/bash appName=`ls|grep .jar$` if [ -z $appName ] then echo "Please check that this script ...
- oracle_fdw安装及使用(无法访问oracle存储过程等对象)
通过oracle_fdw可以访问oracle中的一些表和视图,也可以进行修改,尤其是给比较复杂的系统使用非常方便. (但不能使用oracle_fdw来访问oracle的存储过程.包.函数.序列等对象) ...
- C++ 14 auto
C++14标准最近刚被通过,像以前一样,没有给这个语言带来太大变化,C++14标准是想通过改进C++11 来让程序员更加轻松的编程,C++11引入auto关键字(严格来说auto从C++ 03 开始就 ...
- js模拟自动化测试 -- 多用户登录
1.核心登录提交方法 /** * 动态表单提交方法 * @param url{string}: 提交地址 * @param params{object}: 要提交的表单数据 **/ function ...
- shopnc如何配置微信支付和支付宝支付
步骤一,支付宝账号申请 申请支付宝商家账号 ,填写好公司名称,资质,审核过了,然后填写下面这些参数 步骤二 微信支付申请 登陆微信公众平台-企业微信支付,得到商户号,再申请密钥 注意:支付宝加密方式 ...
- linux中几个简单的系统命令(还有一些其他杂项命令)
linux中几个简单的系统命令,其他命令接触到了在补充. 1.ps命令:(process status),提供对进程的一次性查看.以及执行ps命令时那个时刻的进程信息 格式:ps[参数] -e 此参数 ...
- 使用css3的repeating-linear-gradient画虚线
还在用 border-style: dashed 画虚线吗?虽然也是虚线,但是不能控制每一个虚线的宽度 .dashed { height: 1px; background-image: repeati ...
- 【React -- 5/100】 组件复用
组件复用 React组件复用概述 思考:如果两个组件中的部分功能相似或相同,该如何处理? 处理方式:复用相似的功能 复用什么? state 操作state的方法 两种方式: render props模 ...