Binary search.

  1. class Solution {
  2. int _findClosest(vector<int> &A, int v)
  3. {
  4. int s = , e = A.size() - ;
  5. int ret = INT_MAX;
  6. while(s <= e)
  7. {
  8. int mid = (s + e) / ;
  9. int vmid = A[mid];
  10. int dist = abs(vmid - v);
  11. ret = min(ret, dist);
  12.  
  13. if(vmid == v) return ;
  14. if(vmid < v)
  15. {
  16. s = mid + ;
  17. }
  18. else if(vmid > v)
  19. {
  20. e = mid - ;
  21. }
  22. }
  23. return ret;
  24. }
  25. public:
  26. /**
  27. * @param A, B: Two integer arrays.
  28. * @return: Their smallest difference.
  29. */
  30. int smallestDifference(vector<int> &A, vector<int> &B) {
  31. sort(A.begin(), A.end());
  32.  
  33. int ret = INT_MAX;
  34. for(auto vb : B)
  35. {
  36. ret = min(ret, _findClosest(A, vb));
  37. }
  38. return ret;
  39. }
  40. };

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. scrollView滚动(通过代码)

    平时的开发中可能会要求scrollview滚动,一般的方法时通过scrollview.scrollto(0,1000);来实现,但是注意这个方法是在scrollview停止动画之后才能执行的,因为如果 ...

  2. 268. Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  3. Java获得文件的创建时间(精确到秒)

    jni C/C++ 头文件:MyFileTime.h C/C++ code /* DO NOT EDIT THIS FILE - it is machine generated */#include ...

  4. 数据处理项目Beta阶段软件架构建议

    class:Dataserver string serverIP string serverPassword string sqlAccount string sqlPassword bool Dat ...

  5. Codeforces Round #155 (Div. 2)

    A. Cards with Numbers 模拟. B. Jury Size 相当于统计单天最大需要人数,会发生变化的点在区间两端,枚举计算即可. C. Anagram 从小到大枚举字母: 若当前字母 ...

  6. VK Cup 2012 Round 3 (Unofficial Div. 2 Edition)

    VK Cup 2012 Round 3 (Unofficial Div. 2 Edition) 代码 VK Cup 2012 Round 3 (Unofficial Div. 2 Edition) A ...

  7. C 语言中 free() 函数简单分析

    又是一个睡不着的夜晚,现在是凌晨03:16,不知道是不是感冒的原因,头脑并不是清醒,但是就是睡不着.摸着黑打开电脑,洗了杯子抓了点茶叶,然后打开饮水机电源.舍友们都睡着了,我戴着耳机听着轻音乐,也能听 ...

  8. Python正则表达式总结

    正则表达式也一直用,但是没系统的总结过,今天借这个时间梳理一下. Python中的正则表达式操作依靠re模块儿完成. 常用的方法: re.compile(pattern,flags=0) #返回一个编 ...

  9. Linux定时任务Crontab执行PHP脚本

    http://blog.chinaunix.net/uid-7552018-id-182133.html crontab执行php脚本 http://www.jb51.net/article/2913 ...

  10. fasterflect-vs-hyperdescriptor-vs-fastmember-vs-reflection/

    http://www.codewrecks.com/blog/index.php/2008/10/04/expression-tree-vs-reflection/ http://www.codepr ...