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. ReentrantLock获取锁方式解读(转)

    原文地址:http://www.zhihu.com/question/36771163 (一) lock()方法获取锁.如果该锁没有被另一个线程保持,则获取该锁并立即返回,将锁的保持计数设置为 1.如 ...

  2. 【题解】【矩阵】【回溯】【Leetcode】Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  3. <area> 标签

    定义和用法 <area> 标签定义图像映射中的区域(注:图像映射指得是带有可点击区域的图像). area 元素总是嵌套在 <map> 标签中. 注释:<img> 标 ...

  4. 关于limit hashlimit资料整理

    这几天正在捣鼓防火墙,用到了hashlimit模块.Google了一圈发现相关的文档无论英文还 是中文都很少, 所以我就把自己的折腾的心得记录下来吧. hashlimit是iptables的一个匹配模 ...

  5. python--切片--6

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 一.对list进行切片 取一个list的部分元素是非常常见的操作.比如,一个list如下: &g ...

  6. C#部分---arraylist集合、arraylist集合中的object数据转换成int类string类等;间隔时间的表示方法;

    ArrayList和Array的区别: 相同点:1.两者都实现了IList.ICollection.IEnumerable接口:       2.两者都可以使用证书索引访问集合中的元素,包括读取和赋值 ...

  7. 共享内存+互斥量实现linux进程间通信 分类: Linux C/C++ 2015-03-26 17:14 67人阅读 评论(0) 收藏

    一.共享内存简介 共享内存是进程间通信中高效方便的方式之一.共享内存允许两个或更多进程访问同一块内存,就如同 malloc() 函数向不同进程返回了指向同一个物理内存区域的指针,两个进程可以对一块共享 ...

  8. 【转】详解使用tcpdump、wireshark对Android应用程序进行抓包并分析

    原文网址:http://blog.csdn.net/gebitan505/article/details/19044857 本文主要介绍如何使用tcpdump和wireshark对Android应用程 ...

  9. 特征值分解与奇异值分解(SVD)

    1.使用QR分解获取特征值和特征向量 将矩阵A进行QR分解,得到正规正交矩阵Q与上三角形矩阵R.由上可知Ak为相似矩阵,当k增加时,Ak收敛到上三角矩阵,特征值为对角项. 2.奇异值分解(SVD) 其 ...

  10. SharePoint入门识记

    SharePoint站点层次结构: 1.Web Application: 一般创建后对应一个IIS Web Site, 默认创建后是打不开的,因为网站没有任何内容. 2.Site Collection ...