LintCode "The Smallest Difference"
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"的更多相关文章
- LintCode 387: Smallest Difference
LintCode 387: Smallest Difference 题目描述 给定两个整数数组(第一个是数组A,第二个是数组B),在数组A中取A[i],数组B中取B[j],A[i]和B[j]两者的差越 ...
- Smallest Difference(POJ 2718)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6740 Accepted: 18 ...
- POJ 2718 Smallest Difference(最小差)
Smallest Difference(最小差) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given a numb ...
- The Smallest Difference
Given two array of integers(the first array is array A, the second array is arrayB), now we are goin ...
- Smallest Difference(暴力全排列)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10387 Accepted: 2 ...
- POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)
Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...
- 【POJ - 2718】Smallest Difference(搜索 )
-->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数. ...
- poj 2718 Smallest Difference(暴力搜索+STL+DFS)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6493 Accepted: 17 ...
- POJ 2718 Smallest Difference dfs枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
随机推荐
- scrollView滚动(通过代码)
平时的开发中可能会要求scrollview滚动,一般的方法时通过scrollview.scrollto(0,1000);来实现,但是注意这个方法是在scrollview停止动画之后才能执行的,因为如果 ...
- 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- Java获得文件的创建时间(精确到秒)
jni C/C++ 头文件:MyFileTime.h C/C++ code /* DO NOT EDIT THIS FILE - it is machine generated */#include ...
- 数据处理项目Beta阶段软件架构建议
class:Dataserver string serverIP string serverPassword string sqlAccount string sqlPassword bool Dat ...
- Codeforces Round #155 (Div. 2)
A. Cards with Numbers 模拟. B. Jury Size 相当于统计单天最大需要人数,会发生变化的点在区间两端,枚举计算即可. C. Anagram 从小到大枚举字母: 若当前字母 ...
- 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 ...
- C 语言中 free() 函数简单分析
又是一个睡不着的夜晚,现在是凌晨03:16,不知道是不是感冒的原因,头脑并不是清醒,但是就是睡不着.摸着黑打开电脑,洗了杯子抓了点茶叶,然后打开饮水机电源.舍友们都睡着了,我戴着耳机听着轻音乐,也能听 ...
- Python正则表达式总结
正则表达式也一直用,但是没系统的总结过,今天借这个时间梳理一下. Python中的正则表达式操作依靠re模块儿完成. 常用的方法: re.compile(pattern,flags=0) #返回一个编 ...
- Linux定时任务Crontab执行PHP脚本
http://blog.chinaunix.net/uid-7552018-id-182133.html crontab执行php脚本 http://www.jb51.net/article/2913 ...
- fasterflect-vs-hyperdescriptor-vs-fastmember-vs-reflection/
http://www.codewrecks.com/blog/index.php/2008/10/04/expression-tree-vs-reflection/ http://www.codepr ...