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 number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
给定一个数组及一个目标值,找出三个数,使它们的和与目标值的差值最小,输出它们的和。
解法
与上一题3Sum一样,遍历第一个数,然后用双指针算法遍历剩下两个数,随时比较它们与目标值的差值。
class Solution
{
public:
int threeSumClosest(vector<int>& nums, int target)
{
sort(nums.begin(),nums.end());
int diff = 0x7fffffff;
int ans = 0;
for(int i = 0;i < nums.size();i ++)
{
int j = i + 1;
int k = nums.size() - 1;
while(j < k)
{
int sum = nums[i] + nums[j] + nums[k];
if(abs(sum - target) < diff)
{
diff = abs(sum - target);
ans = sum;
}
if(sum < target)
j ++;
else
k --;
}
}
return ans;
}
};
LeetCode 3Sum Closest (Two pointers)的更多相关文章
- [LeetCode]3Sum Closest题解
3sum Closest: Given an array S of n integers, find three integers in S such that the sum is closest ...
- [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
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
Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- leetcode 3Sum Closest python
class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List ...
- LeetCode 3Sum Closest 最近似的3sum(2sum方法)
题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- 3Sum Closest - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 3Sum Closest - LeetCode 注意点 和3Sum那道题的target是0,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...
随机推荐
- 大于2t的磁盘分区,并格式化ext4挂载
1:MBR分区表:(MBR含义:主引导记录) 所支持的最大卷:2T (T; terabytes,1TB=1024GB) 对分区的设限:最多4个主分区或3个主分区加一个扩展分区. 2:GPT分区表:(G ...
- 单点登录SSO的实现原理 (转)
单点登录SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户的一次登录能得到其他所有系统的信任.单点登录在大型网站里使用得 ...
- .net反编译工具
1:.Net Reflector [收费]官方网址:http://www.red-gate.com/products/dotnet-development/reflector/ 2:ILSpy/dnS ...
- python爬虫_入门_翻页
写出来的爬虫,肯定不能只在一个页面爬,只要要爬几个页面,甚至一个网站,这时候就需要用到翻页了 其实翻页很简单,还是这个页面http://bbs.fengniao.com/forum/10384633. ...
- chattr -lsattr 文件加锁解锁简单用法
chattr: 加锁文件,无修改,无删除权限. 常用参数: +a: 可给文件追加内容,但无法删除. +i 加锁文件(文件不能被删除.改名.设定链接关系,同时不能写入或追加内容) -i ...
- tomcat 闪退处理
参考文章:https://www.cnblogs.com/lanjianhappy/p/6491436.html 首先确保JDK环境变量已经配置好! 设置jdk环境 变量:https://jingya ...
- 另开一篇 https
https 流程 1.加密传输:对称加密传输信息 2.身份认证:非对称加密.通过证书来保障客户端给服务器的密钥唯一性. 因为中间层要是伪装公钥和证书,但是又无法解密原有的发送的数据,那么发给服务器的数 ...
- 使用C#获取Windows Phone手机的各种数据(转)
转自:http://www.sum16.com/desinger/use-c-sharp-get-windows-phone-information.html 使用C#获取Windows Phone手 ...
- Android 生态消息推送平台介绍
一.手机厂商平台 华为消息推送服务 华为推送(Push)是为开发者提供的消息推送平台,建立了从云端到手机端的消息推送通道,使应用可以将最新信息及时通知用户,从而构筑良好的用户关系,提升用户的感知和活跃 ...
- treap入门
这几天刚学了treap,听起来还行,就是调题调到恶心了…… 就以这道题作为板子吧(”你本来也就做了一道题!”) https://www.luogu.org/problemnew/show/P3369 ...