二手和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. Hongwei Xi

    Hongwei Xi Hongwei Xi Hongwei Xi's Curriculum Vita Hongwei Xi

  2. java(样品集成框架spring、spring mvc、spring data jpa、hibernate)

    这是你自己的参考springside集成框架的开源项目.主要的整合spring.spring mvc.spring data jpa.hibernate几个框架,对于这些框架中仍然感觉更舒适sprin ...

  3. Swift - 使用表格组件(UITableView)实现分组列表

    1,样例说明: (1)列表以分组的形式展示 (2)同时还自定义分区的头部和尾部 (3)点击列表项会弹出消息框显示该项信息. 2,效果图:       3,代码如下: 1 2 3 4 5 6 7 8 9 ...

  4. 应用程序初始化正常(0xc015002)失败解决方法

    VS2005 sidebyside manifest error Microsoft.VC80.MFC Microsoft.VC80.CRT Microsoft.VC80.MFCLOC msvcr80 ...

  5. 11 款最好 CSS 框架 让你的网站独领风骚

    网页设计和发展领域已经成为竞争激烈的虚拟世界.想要在网络的虚拟世界中生存,仅有一堆静止的在线网络应用是远远不够的,网页必须要有很多功能,配以让人无法抗拒的设计.网页编码一定要合适.精确,才能保证不发生 ...

  6. Python语法

  7. 硬盘重装Ubuntu12.04的感受

    好久没更blog了,最近这两天系统也出了问题,win7蓝屏,ubuntu进不去-.后来win7整好了,ubuntu依旧顽固.用惯了linux,就不想在转到win7下面了,估计是习惯了各种敲命令的感觉吧 ...

  8. 【iOS发展-44】通过案例谈iOS重构:合并、格式化输出、宏观变量、使用数组来存储数据字典,而且使用plist最终的知识

    我们今天的情况下是第一个例子,下面的5一来通过切换页上一页下一页: (1)第一步,基本是以非常傻非常直接的方式来创建.这里用到的主要点有: --把对象变量设置为全局变量使得能够在其它方法中调用来设置它 ...

  9. Android源代码分析-资源载入机制

    转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/23387079 (来自singwhatiwanna的csdn博客) 前言 我们 ...

  10. cocos2d-x 类大全及其概要

    CCNode 节点类是Cocos2D-x中的主要类,继承自CCObject. 任何需要画在屏幕上的对象都是节点类.最常用的节点类包括场景类(CCScene).布景层类(CCLayer).人物精灵类(C ...