16.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.
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).
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int size = nums.size(); sort(nums.begin(), nums.end());
diff = INT_MAX;
for(int i = ; i < size-; i++){
if(i> && nums[i]==nums[i-]) continue;
find(nums, i+, size-, target-nums[i]);
if(diff == ) return target;
}
return target+diff;
} void find(vector<int>& nums, int start, int end, int target){
int sum;
while(start<end){
sum = nums[start]+nums[end];
if(sum == target){
diff = ;
return;
}
else if(sum>target){
do{
end--;
}while(end!=start && nums[end] == nums[end+]);
if(sum-target < abs(diff)) diff = sum - target;
}
else{
do{
start++;
}while(start!= end && nums[start] == nums[start-]);
if(target - sum < abs(diff)) diff = sum - target; //不能只在最后检查:可能会有这种情况,前一次sum>target,这次sum<target,而且下次就start==end,那么很可能前一次的sum比这次的sum更接近target
}
}
}
private:
int diff; //how much bigger is the sum
};
16.3Sum Closest (Two-Pointers)的更多相关文章
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- 《LeetBook》leetcode题解(16):3Sum Closest [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- 15. 3Sum、16. 3Sum Closest和18. 4Sum
15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...
- 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]
题目 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 最近三数之和
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 ...
随机推荐
- 网络编程I/O函数介绍
read和write #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); ssize_t write(in ...
- Python基础知识记录
1.去除空格 strip() 删除两边的空格.lstrip() 删除左边的空格.rstrip() 删除右边的空格 2.字符串的连接 s1='abc' s2='cdf' s3=s1+s2 pr ...
- python3 随机生成UserAgent
安装库 pip install fake_useragent #引入 from fake_useragent import UserAgent; ua = UserAgent(); print(ua. ...
- 490 - Rotating Sentences
Rotating Sentences In ``Rotating Sentences,'' you are asked to rotate a series of input sentences ...
- Linux下驱动模块学习
1.modutils中提供了相关的insmod,rmmod,modinfo工具2.modprobe在识别出目标模块所依赖模块后也是调用insmod.3.从外部看模块只是普通可重定位的目标文件.可重定位 ...
- Chrome 上传时打开对话框非常慢
Chrome 上传时打开对话框非常慢 先说解决方法,将 Chrome 中这个选项关闭,打开会飞快. 如果只是图片之类是不会的,但是有 zip apk 之类的就会慢. 主要原因还是 Chrome 太安全 ...
- WCF揭秘学习笔记(2):数据表示
背景知识 WCF提供了一种语言为软件通信建模,称作服务模型.使用更底层的编程架构提供的类可以从这种语言建立的模型中生成可用的通信软件. 在服务模型使用的语言中,负责通信的软件部分称为服务(servic ...
- 黄聪:VS2010编辑C#未启动,打开设计视图时报"未将对象引用设置到对象的实例"
通常情况下,若是你将用户控件写好了放入窗体中,若是有不合理的代码,则会弹出错误提示框,不让你放.若是你之前只是随便加了一个用户控件,并且没有什么问题,但后来你又把控件改坏掉了,那么你打开就会报错(在窗 ...
- MAMP pro mac 本地集成环境 php sal apache等集成软件
http://www.sdifen.com/mamppro411.html 已存在我的百度云盘 安装后,打开 MAMP 第一步:配置启动和停止选项 默认启动项.默认停止项,只需要勾选: 1.Star ...
- C# 通过Exchange server 发送邮件
微软的Exchange邮件服务不同与一般的邮件server,他不能简单使用SmtpClient等组件实现邮件收发的功能. 那么怎么通过Exchange服务发送邮件呢? 微软的Exchange服务都有w ...