Binary search.

class Solution {
int _findClosest(vector<int> &A, int v)
{
int s = , e = A.size() - ;
int ret = INT_MAX;
while(s <= e)
{
int mid = (s + e) / ;
int vmid = A[mid];
int dist = abs(vmid - v);
ret = min(ret, dist); if(vmid == v) return ;
if(vmid < v)
{
s = mid + ;
}
else if(vmid > v)
{
e = mid - ;
}
}
return ret;
}
public:
/**
* @param A, B: Two integer arrays.
* @return: Their smallest difference.
*/
int smallestDifference(vector<int> &A, vector<int> &B) {
sort(A.begin(), A.end()); int ret = INT_MAX;
for(auto vb : B)
{
ret = min(ret, _findClosest(A, vb));
}
return ret;
}
};

LintCode "The Smallest Difference"的更多相关文章

  1. LintCode 387: Smallest Difference

    LintCode 387: Smallest Difference 题目描述 给定两个整数数组(第一个是数组A,第二个是数组B),在数组A中取A[i],数组B中取B[j],A[i]和B[j]两者的差越 ...

  2. Smallest Difference(POJ 2718)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6740   Accepted: 18 ...

  3. POJ 2718 Smallest Difference(最小差)

     Smallest Difference(最小差) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given a numb ...

  4. The Smallest Difference

    Given two array of integers(the first array is array A, the second array is arrayB), now we are goin ...

  5. Smallest Difference(暴力全排列)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10387   Accepted: 2 ...

  6. POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)

    Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...

  7. 【POJ - 2718】Smallest Difference(搜索 )

    -->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数. ...

  8. poj 2718 Smallest Difference(暴力搜索+STL+DFS)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6493   Accepted: 17 ...

  9. POJ 2718 Smallest Difference dfs枚举两个数差最小

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19528   Accepted: 5 ...

随机推荐

  1. RelativeLayout相对布局

    RelativeLayout相对布局常用属性: 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_center ...

  2. elfutils-libelf由于依赖而安装失败

    在Redhat安装Oracles前需要按照依赖包,但是在安装elfutils-libelf遇到了两个包相互依赖的情况 [root@rhvm1 /]# rpm -i elfutils-libelf-de ...

  3. iOS学习笔记---oc语言第一天

    第一讲 初始类和对象 c语言的超集,允许在oc中使用c语言源代码.编译器兼容c语言程序 具备完善的面向对象特性 包含一个运行时系统 类库丰富 面向对象编程 oop 面向对象语言:c++  java   ...

  4. ZOJ 1047 Image Perimeters

    原题链接 题目大意:鼠标点击一块,求与之联通的所有区域的边长之和. 解法:广度优先搜索.从选中的这个点开始,往周围8个点依次搜索,访问过的点做上标记.如果该点上下左右的一个或多个方向没有相邻的点,边长 ...

  5. pascal矩阵

    帕斯卡矩阵 1.定义       帕斯卡矩阵:由杨辉三角形表组成的矩阵称为帕斯卡(Pascal)矩阵. 杨辉三角形表是二次项 (x+y)^n 展开后的系数随自然数 n 的增大组成的一个三角形表. 如4 ...

  6. 如何卸载rpm包

    首先通过  rpm -q <关键字> 可以查询到rpm包的名字 然后 调用 rpm -e <包的名字> 删除特定rpm包 如果遇到依赖,无法删除,使用 rpm -e --nod ...

  7. DELPHI WEBSERVICE

    一.服务程序 1.依次选择 NEW -> OTHER -> WEB SERVICE -> SOAP SERVER APPLICATION -> ISAPI DYNAMIC LI ...

  8. ABB机器人添加串口模块后无法使用的解决办法

    [环境] ABB机器人1520,IRC5,RobotWare5.6,Win10 64bits,RobotStudio6.0 [过程和表现] 由于项目需要和机器人通信,DeviceNet又不能满足要求, ...

  9. strlen() 函数

    strlen() 函数通常用来计算字符串的长度,但是今天突然发现个奇怪的现象. 如下所示: #include <stdio.h> #include <stdlib.h> #in ...

  10. (转) ICCV 2015:21篇最火爆研究论文

          ICCV 2015:21篇最火爆研究论文 ICCV 2015: Twenty one hottest research papers   “Geometry vs Recognition” ...