二手和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的更多相关文章

  1. LeetCode——3Sum &amp; 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 ...

  2. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. LeetCode 3Sum Smaller

    原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...

  7. leetcode — 3sum

    import java.util.*; /** * Source : https://oj.leetcode.com/problems/3sum/ * * Created by lverpeng on ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. hadoop 磁盘限额配置

    配置方法: 在 hdfs-site.xml 里配置如下参数,注意,那个 value 的值是配置该磁盘保留的DFS不能使用的空间大小,单位是字节. (如果多块硬盘,则表示为每块硬盘保留这么多空间) &l ...

  2. 如何得到动态链接库的输出函数tdump命令(225篇博文)

    有的时候,我们需要查看一个动态链接库的输出函数列表,有很多软件可以满足此要求,比如说 exeScope.不过,去下载一个软件总归是很麻烦,Delphi 本身就自带一个类似的工具,那就是 tdump.e ...

  3. 14.5.4 InnoDB File-Per-Table Tablespaces 每个表一个文件

    14.5.4 InnoDB File-Per-Table Tablespaces 每个表一个文件 从历史上看, 所有的InnoDB 表和索引是存储在system 表空间, 这个整体的方法是针对机器专注 ...

  4. Python的对象和类型

    Python使用对象来存储数据,构造任何类型的值都是一个对象. 任何一个对象都有三个特性:身份,类型和值. 身份是对象的唯一标识,可以通过内建函数id()得到,这个值可以认为是该对象的内存地址. Py ...

  5. Redis slowlog

    和mongo的slowlog一样,redis中对于操作时间较长(默认为10秒)的命令也会记录下来,不过它将它们保存在redisServer结构中的slowlog这个链表中,新进来的log排在链表头部, ...

  6. Android 实现自己定义多级树控件和全选与反选的效果

    博文開始之前,首先要感谢大牛:(lmj623565791),本博文是在其博文http://blog.csdn.net/lmj623565791/article/details/40212367基础上进 ...

  7. python基础教程_学习笔记10:异常

    异常 什么是异常 Python用异常对象来表示异常情况.遇到错误后,会引发异常.假设异常对象并未被处理或捕捉,程序就会用所谓的回溯(Traceback,一种错误信息)终止运行: >>> ...

  8. Enum的简单了解

    Enum可以将一组具名的有限集合创建成一种新的类型,而这些具名的值可以作为常规的程序组件使用. 在创建enum时,编译器会为你生成一个相关的类,这个类继承自java.lang.Enum,所以enum本 ...

  9. JAVA实现Shell排序

    Shell排序可以理解为插入排序的变种,它充分利用了插入排序的两个特点: (1). 当数据规模小的时候非常高效. (2). 当给定数据已经有序时的时间代价为O(N) 所以,Shell排序每次把数据分成 ...

  10. Linux Shell脚本入门--grep命令详解

    grep简介<摘自鸟哥,并加以整理.> grep (global search regular expression(RE) and print out the line,全面搜索正则表达 ...