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 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
随机推荐
- hadoop_批量命令脚本&同步文件脚本
1.xcall.sh 批量命令脚本,例:xcall.sh jps ,查看hadoop101~ hadoop104的jps进程 注意:在执行命令的时候,若是提示没有这个命令,但是在本机又可以执行,记得在 ...
- HDU1069(KB12-C)
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 关于shortcut icon和icon
语句一:<link rel="shortcut icon" href="favicon.ico" /> 语句二<link rel=" ...
- 如何用JS获取页面上的所有标签
最近忙的一匹,忙着大保健,都来不及写博客,今天特意抽出点时间来写一写 前两天看到一个题,是问如何从页面上获取所有的标签的并查看他们的数量,感觉还是有点意思的,所以给大家来搞一下子 我们先来捋捋思路,那 ...
- Excel indirect引用其它xlsx文件内容作为下拉框
效果如下图: 在第一个excel文件中有一个下拉框 这里面的选项,需要从另外一个Excel文件中读取内容,另外一个Excel文件如下: 实现的步骤如下: 1.新建一个Excel文件select.xls ...
- Linux —— Vi 命令介绍
简介 vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器. 这里只是简单地介绍一下它的用法和一小部分指令. 由于对Unix及Linux系统的任何版本,vi编辑 ...
- synchronized的四种用法
一 修饰方法 Synchronized修饰一个方法很简单,就是在方法的前面加synchronized,synchronized修饰方法和修饰一个代码块类似,只是作用范围不一样,修饰代码块是大括号括起 ...
- Oracle EBS AP 应付核销到确定一行预付款
-- purpose: 应付标准发票核销预付款发票中的一行 -- 12.2.6 环境 -- author:jenrry create_date: 2017-06-08 declare l_error_ ...
- css继承属性与非继承属性
一.无继承性的属性 1.display:规定元素应该生成的框的类型 2.文本属性: vertical-align:垂直文本对齐 text-decoration:规定添加到文本的装饰 text-shad ...
- 判断Exception类中是否有InnerException属性
public static class ExceptionExtend { /// <summary> /// 利用反射来判断对象是否包含某个属性 /// </summary> ...