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 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
随机推荐
- python学习之老男孩python全栈第九期_day012知识点总结
# def wrapper(f):# def inner(*args,**kwargs):# print('在被装饰的函数执行之前做的事')# res = f(*args,**kwargs)# pri ...
- ajax jsonp的跨域请求
1.页面ajax的请求 $.ajax({ async: false, url: 'http://localhost:8080/downloadVideos',//跨域的dns/document!sea ...
- MySql导出sql语句
sql解释: mysqldump 是mysql的一个专门用于拷贝操作的命令 --opt 操作的意思 --compress 压缩要传输的数据 --skip-lock 忽略锁住的表(加上这句能防止当表有外 ...
- 学习ES6的全部特性
ES6 简介 ECMAScript 6 简称 ES6,是 JavaScript 语言的下一代标准,已经在2015年6月正式发布了.它的目标是使得 JavaScript 语言可以用来编写复杂的大型应用程 ...
- 高性能JavaScript(高性能Ajax)
ajax是一种与服务器通信而无需重载页面的方法(即局部刷新.) 高性能的Ajax应该考虑数据传输技术和数据格式,以及其他的如数据缓存等优化技术. 请求数据 请求数据的常用技术有XMLHttpReque ...
- OSGI企业应用开发(二)Eclipse中搭建Felix运行环境
上篇文章介绍了什么是OSGI以及使用OSGI构建应用的优点,接着介绍了两款常用的OSGI实现,分别为Apache Felix和Equinox,接下来开始介绍如何在Eclipse中使用Apache Fe ...
- Nginx控制并发连接数
ngx_http_limit_conn_module这个模块用于限制每个定义的key值的连接数,特别是单IP的连接数. 不是所有的连接数都会被计数.一个符合计数要求的连接是整个请求头已经被读取的连接. ...
- WOSA/XFS PTR Form解析库—头文件
class AFX_EX_CLASS CNuXfsForm {public: CNuXfsForm(); ~CNuXfsForm(); /******************************* ...
- VideoView获取本地视频播放
主布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...
- Visual Studio编译C工程出现的错误
错误1. エラー 1 error LNK1561: エントリー ポイントを定義しなければなりません. 解决办法:将工程的类型改为dll动态库,设置方式如下: 右键工程,选择[プロパティ].在弹出的面板 ...