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 ...
随机推荐
- c#Enum的用法
public enum ResType { Role = 0, Dept = 1, Group = 2, Site = 3, Org = 4, Sub=8 } 这里定义了一个enum ResTy ...
- cannot run program "git.exe":CreateProcess error=2
在使用android studio从git上check项目的时候报错cannot run program "git.exe":CreateProcess error=2 请检查下面 ...
- Dom4j和Xpath(转)
1.DOM4J简介 DOM4J是 dom4j.org 出品的一个开源 XML 解析包.DOM4J应用于 Java 平台,采用了 Java 集合框架并完全支持 DOM,SAX 和JAXP. DOM4J使 ...
- cape town
开普敦_百度百科 开普敦
- hdu4055 Number String
Number String Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- [SVN]两个分支合并
Date:2014-1-1 Summary: 记录一下自己使用SVN时候的操作步骤,先吃鱼,再学钓鱼 Contents: 环境:从同事的branch迁出一份代码,作为自己的分支进行开发,同时同事也在自 ...
- [Cocos2d-x]节点的尺寸大小
作为一个CCNode,本身没有大小而言,但是AddChild之后,便有了尺寸的概念. Cocos2d-x中对于一个节点的尺寸可以通过以下三个方法获取: CCSprite: getContentSize ...
- Kaggle—Digit Recognizer竞赛
Digit Recognizer 手写体数字识别 MNIST数据集 本赛 train 42000样例 test 28000样例,原始MNIST是 train 60000 test 10000 我分别 ...
- 定义自己的布局RelativeLayout
绘制网格线
在Android画线必须由一个载体,无论是控制,无论是布局.实际上它们是从继承View.由画线的方式自己的控制或布局的定义是最常见的. 以下是在其定义中的小样本实现RelativeLayout绘制网络 ...
- 在RHEL上安装Thrift(支持C++)的若干问题 » 编码无悔 / Intent & Focused
在RHEL上安装Thrift(支持C++)的若干问题 » 编码无悔 / Intent & Focused [原创]在RHEL上安装Thrift(支持C++)的若干问题 2010年12月1 ...