leetcode16—3 Sum Closet
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).
想法:与之前的类似,使用双指针,另外设立一个变量去保存最接近target的值
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int len = nums.size();
> len)
;
) + nums.at() + nums.at();
sort(nums.begin(),nums.end());
; i < len- ; i++){
&& nums[i] == nums[i-])
continue;
;
;
while(left < right){
int sum = nums.at(i) + nums.at(left) + nums.at(right);
if(sum == target)
return sum;
if(abs(sum - target) < abs(temp - target)){
temp = sum;
}
if(sum < target){
left++;
}else{
right--;
}
}
}
return temp;
}
};
leetcode16—3 Sum Closet的更多相关文章
- LeetCode_3 sum closet
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数组解题模板
一.模板以及题目分类 1.头尾指针向中间逼近 ; ; while (pos1<pos2) { //判断条件 //pos更改条件 if (nums[pos1]<nums[pos2]) pos ...
- leetcode 之Sum系列(七)
第一题是Two Sum 同样是用哈希表来做,需要注意的是在查打gap是要排除本身.比如target为4,有一个值为2,gap同样为2. vector<int> twoSum(vector& ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
随机推荐
- Android-View的绘制源码学习总结
##前言 算是第一篇正式的github博文,回顾了一下之前看过的view源码解析,做一个对目前为止View学习小的总结. 我觉得对于源码的解析和学习,把所有流程记下来意义并不是很大,最关键的是: 1. ...
- 【代码笔记】iOS-使用MD5加密
一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, ...
- drupal 去掉视图中字段默认的HTML标签
1.格式--设置 去掉复选框 2.具体字段:
- Windows7系统如果安装&升级IE11浏览器
作为一个前端工作人员,IE678简直就是噩梦,还好现在大多数网站已经开始放弃了对IE6/7/8的支持了. 由于Win7系统默认是安装的IE8,所以在打开部分网站时会提示:IE浏览器版本过低.解决方法如 ...
- SD从零开始03-04
[原创]SD从零开始3 SD中的主数据 客户主数据Customer master(分层维护) 一般数据general data: 与销售和财务都有关,对所有的组织单元有效: 销售区域数据sales a ...
- 判断exe是64位还是32位
右击exe属性,查看兼容模式. 如果有windwos vista之前的版本则为32位的,如下图: 如果没有windwos vista之前的版本则为64位的,如下图:
- Pytest+Allure环境的搭建
参考博客 测试报告解释 pytest+allurre进阶 1. pytest的安装: 1.1. windows下: pip install pytest 1.2. linux下: pip instal ...
- ChatOps如何变革企业业务
[编者按]本文作者为日志分析软件公司 Logz.io 的联合创始人 Tomer Levy,主要介绍 ChatOps 的特点与发展历程,以及将来可能带来的业务变革.文章系国内 ITOM 管理平台 One ...
- MemSQL 架构初探
MemSQL 自称是最快的内存数据库.目前已发布了2.5版本. MemSQL 具有以下特点 1 高效的并行,尤其是分布式的MemSQL. 2 高效的并发,采用lock-free的内存数据结构skip ...
- maven+spring+junit测试要注意的事情
使用maven方式创建webapp工程的资料网上一大堆,在这里也不详细说了.在创建完成之后,里面说到要转动态web工程时要切换为3.0版本,但是我本地切换不了,网上的方法好像也没用,暂时也没用到这块. ...