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 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 = {- -}, and target = .
- The sum that is closest to the target is . (- + + = ).
3 sum 的变形,这里不需要考虑重复而已
- class Solution {
- public:
- int threeSumClosest(vector<int> &num, int target) {
- // Start typing your C/C++ solution below
- // DO NOT write int main() function
- sort(num.begin(), num.end());
- int len = num.size();
- if(len < ) return ;
- int res = num[] + num[] + num[];
- for(int i = ; i <= len -; ++i){
- int low = i+;
- int high = len -;
- while(low < high){
- int sum = num[i] + num[low] + num[high];
- if(sum == target)
- return target;
- else if(sum < target){
- ++low;
- if(abs(sum - target) < abs(res - target))
- res = sum;
- }else{
- --high;
- if(abs(sum - target) < abs(res - target))
- res = sum;
- }
- }
- }
- return res;
- }
- };
LeetCode_3 sum closet的更多相关文章
- LeetCode_3 sum
Given an array S of n integers, are there elements a, b, c ? Find all unique triplets in the array w ...
- leetcode16—3 Sum Closet
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- 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 ...
随机推荐
- kindle
http://www.mindmap8.com/Kindle_Paperwhite/20130726266.html http://blog.csdn.net/felomeng/article/det ...
- linux下安装apache2.2.27
1.首先下载httpd-2.2.27.tar.gz用linux命令下载 wget http://mirrors.cnnic.cn/apache//httpd/httpd-2.2.27.tar.gz 2 ...
- C#面向对象(二)
一:抽象方法 1. 在面向对象编程语言中抽象方法指一些只有方法声明,而没有具体方法体的方法.抽象方法一般存在于抽象类或接口中. 在一些父类中,某些行为不是非常明确,因此无法用代码来具体实现,但是类还必 ...
- 求职,找工作,平台大PK
国内 猎聘网:www.lietou.com 拉钩网:Lagou.com 智联招聘:www.zhaopin.com 前程无忧:http://www.51job.com/ 中华英才网:chinahr.co ...
- 在C#中实现Socket端口复用
转载:http://www.csharpwin.com/csharpspace/68.shtml 一.什么是端口复用: 因为在winsock的实现中,对于服务器的绑定是可以多重绑定的,在 ...
- C#调用ActiveX控件
背景:最近项目中需要用到ActiveX控件,项目是在.Net平台下开发的.因此就直接在项目中添加了对ActiveX控件的引用,添加引用成功.在代码中实例化类的实例也没有问题,但在调用其方法或属性时总是 ...
- .NET下的加密解密大全(1): 哈希加密
.NET有丰富的加密解密API库供我们使用,本博文总结了.NET下的Hash散列算法,并制作成简单的DEMO,希望能对大家有所帮助. MD5[csharp]using System; using Sy ...
- latch: cache buffers chains故障处理总结
一大早就接到开发商的电话,说数据库的CPU使用率为100%,应用相应迟缓.急匆匆的赶到现场发现进行了基本的检查后发现是latch: cache buffers chains 作祟,处理过程还算顺利,当 ...
- spring源码_下载以及转入eclipse (2016-11-08)
本例spring源码版本是4.3.0的, 所以jdk需要准备1.8的(不同版本源码要求的jdk不一样) 1.8版本myeclipse10无编译环境,只有运行环境,出现点问题,下载最新版本的Eclips ...
- COM简单应用示例
使用com技术开发模式进行的示例. com技术关键部分源码:主要将所有接口都写入到这个文件中 testinterface.h #ifndef TESTINTERFACE_H #define TESTI ...