Leetcode 3Sum Closet
二手和3Sum像几乎相同的想法。二进制搜索。关键修剪。但是,在修剪做出很多错误。
然后还有一个更加速了原来的想法O(n^2).
#include<iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std; class Solution {
public: int threeSumClosest(vector<int> &num, int target) { int minDis = 1 << 30;
int closetSum = -1<<30; sort(num.begin(), num.end());
for (int i = 0; i < num.size() - 2; i++)
{
for (int j = i + 1; j < num.size() - 1; j++)
{ int twoSum = num[i] + num[j];
int value = target - twoSum; int tmpCloVal = searchValue(num, j + 1, num.size() - 1, value); if (abs(target - twoSum - tmpCloVal) < minDis)
{
closetSum = twoSum + tmpCloVal;
minDis = abs(target - closetSum);
} if ( num[j+1]>0 && num[i]+num[j] > target)
break;
} if (num[i+1]>0 && num[i] > target)
break;
} return closetSum;
} int searchValue(vector<int> num, int left, int right, int value)
{
const int l = left, r = right;
int closetVal;
int m; if (value > num[right])
return num[right];
else if (value < num[left])
return num[left]; while (left <= right){
m = (left + right) / 2; if (num[m] <= value && num[m + 1] >= value)
{
break;
} else if (num[m] < value)
{
left = m + 1;
}
else
{
right = m - 1;
}
} closetVal = num[m]; if (abs(closetVal-value) > abs(num[m + 1] - value))
{
closetVal = num[m+1];
}
if (abs(closetVal - value) > abs(num[m-1]-value))
{
closetVal = num[m - 1];
} return closetVal;
} };
版权声明:本文博主原创文章。博客,未经同意不得转载。
Leetcode 3Sum Closet的更多相关文章
- LeetCode——3Sum & 3Sum Closest
3Sum 题目 Given an array S of n integers, are there elements a,b,c in S such that a + b + c = 0? Find ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [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] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 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 3Sum Smaller
原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...
- leetcode — 3sum
import java.util.*; /** * Source : https://oj.leetcode.com/problems/3sum/ * * Created by lverpeng on ...
- LeetCode: 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
随机推荐
- 在SharePoint 2010中部署RBS (转)
一.RBS(Remote BLOB Storage)简单介绍 在SharePoint的大部分企业应用案例中,SharePoint都是要承担着非常繁重的文件管理工作,这些文件类型包含了Word文档,Ex ...
- vs2008编译QT开源项目三国杀(五篇文章)
请参看 http://tieba.baidu.com/f?kz=1508964881 按照上面的网址教程,下载三国杀源码,swig工具,并下载最新的QT4.8.2 for vs2008.我本机已经安装 ...
- KMP算法详解(转自中学生OI写的。。ORZ!)
KMP算法详解 如果机房马上要关门了,或者你急着要和MM约会,请直接跳到第六个自然段. 我们这里说的KMP不是拿来放电影的(虽然我很喜欢这个软件),而是一种算法.KMP算法是拿来处理字符串匹配的.换句 ...
- wamp环境中mysql更改root密码
集成的wamp环境命令不好使,在phpmyadmin中更改密码 权限——root用户修改,执行 然后,你会发现你不能用phpmyadmin登陆了修改下phpmyadmin里面对应的密码就可以了将php ...
- ThinkPHP页面跳转、Ajax技巧详细介绍(十八)
原文:ThinkPHP页面跳转.Ajax技巧详细介绍(十八) ThinkPHP页面跳转.Ajax技巧详细介绍 一.页面跳转 $this->success('查询成功',U('User/test' ...
- mvc action 有多种跳转
在ASP.NET mvc下,action 有多种跳转方式: return RedirectToAction("Index");//一个参数时在本Controller下 如果Redi ...
- VSTO 为Office已有右键菜单添加自己的菜单项(word,Excel)
原文:VSTO 为Office已有右键菜单添加自己的菜单项(word,Excel) private void AddRightMenu() { Microsoft ...
- Linux SSH常用总结(转)
一.连接到远程主机 格式: ssh name@remoteserver 例如: ssh ickes@192.168.27.211 二.连接到远程主机指定的端口 格式: ssh name@remotes ...
- Android实战简易教程-第九枪(BitmapFactory.Options对资源图片进行缩放)
我们知道,我们编写的应用程序都是有一定内存限制的.程序占用了过高的内存就easy出现OOM(OutOfMemory)异常.因此在展示高分辨率图片的时候,最好先将图片进行压缩,压缩后的图片大小应该和用来 ...
- 解决com.ibatis.sqlmap.client.SqlMapException: There is no statement named in this SqlMap
com.ibatis.sqlmap.client.SqlMapException: There is no statement named in this SqlMap. 可能存在3种情况: 1.在x ...