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 ...
随机推荐
- POJ 1862 & ZOJ 1543 Stripies(贪心 | 优先队列)
题目链接: PKU:http://poj.org/problem?id=1862 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...
- Swift - 类型属性(类静态属性)和类方法(类静态方法)
1,结构体struct和枚举enum的静态属性,静态方法使用static关键字 1 2 3 4 5 6 7 8 9 10 struct Account { var amount : Doub ...
- Linux程序设计学习笔记----多线程编程线程同步机制之相互排斥量(锁)与读写锁
相互排斥锁通信机制 基本原理 相互排斥锁以排他方式防止共享数据被并发訪问,相互排斥锁是一个二元变量,状态为开(0)和关(1),将某个共享资源与某个相互排斥锁逻辑上绑定之后,对该资源的訪问操作例如以下: ...
- Linux入门基础 #10:命令行文本处理工具
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- Function 详解(一)
一直想写一系列关于javascript的东西,可惜从申请博客以来就一直抽不出时间来好好写上一番,今天终于熬到周末,是该好好整理一下,那么先从声明函数开始吧; 总所周知,在javascript中有匿名函 ...
- UVALive 6869(后缀数组)
传送门:Repeated Substrings 题意:给定一个字符串,求至少重复一次的不同子串个数. 分析:模拟写出子符串后缀并排好序可以发现,每次出现新的重复子串个数都是由现在的height值减去前 ...
- MQ、JMS以及ActiveMQ
MQ简介: MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过写和检索出入列队的针对应用程序的数据(消息)来通信,而无需专用连接来链接它们.消息传 ...
- 浅谈 Python 程序和 C 程序的整合
源地址:http://www.ibm.com/developerworks/cn/linux/l-cn-pythonandc/ 概览 Python 是一种用于快速开发软件的编程语言,它的语法比较简单, ...
- 浅谈 PHP 变量可用字符
原文:浅谈 PHP 变量可用字符 先来说说php变量的命名规则,百度下一抓一大把:(1) PHP的变量名区分大小写;(2) 变量名必须以美元符号$开始;(3) 变量名开头可以以下划线开始;(4) 变量 ...
- [Cocos2d-x]布局与定位
游戏中,精灵的位置由Position与AnchorPoint同时决定. Scene 锚点 (0,0) 不启用锚点 CCNode锚点 (0,0) 不启用锚点 CCLayer锚点 (0,0) 不启用锚点 ...