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 ...
随机推荐
- RESTful 杂文归集
Json格式与对象转换 org.codehaus.jackson.map.ObjectMapper 工具转json格式,进行接口文档书写工具http://editor.swagger.io/#/ ja ...
- dos快速通道
要在文件夹的右键菜单中添加“命令提示符”选项.在注册表HKEY_CLASSES_ROOT\Directory\shell分支下新建一项“CommandPrompt”,修改右侧窗格中的“默认”键值为“命 ...
- 【转载】GBDT(MART) 迭代决策树入门教程 | 简介
转载地址:http://blog.csdn.net/w28971023/article/details/8240756 GBDT(Gradient Boosting Decision Tree) 又叫 ...
- box2dweb 学习笔记--sample讲解
前言: 之前博文"台球游戏的核心算法和AI(1)" 中, 提到过想用HTML5+Box2d来编写实现一个台球游戏. 以此来对比感慨一下游戏物理引擎的巨大威力. 做为H5+box2d ...
- jQuery弹出层始终垂直居中相对于屏幕或当前窗口
把弹出层的位置设为fixed,设置top:50%,然后获取当前元素的整体的高度height,用获取的高度height/2,设置margin-top:-height/2.即可把当前的弹出层始终垂直居中于 ...
- Validform:一行代码搞定整站的表单验证!
表单验证不再发愁,http://validform.rjboy.cn/
- Spring MVC程序中得到静态资源文件css,js,图片文件的路径问题总结
上一篇 | 下一篇 Spring MVC程序中得到静态资源文件css,js,图片 文件的路径 问题总结 作者:轻舞肥羊 日期:2012-11-26 http://www.blogjava.net/fi ...
- POJ-2991 Crane(区间更新+向量旋转)
题目大意:n个向量首尾相连,每次操作使某个区间中的所有向量都旋转同样的角度.每次操作后都回答最后一个向量的坐标. 题目分析:区间维护向量信息.向量旋转:x1=x0*cos(t)-y0*sin(t),y ...
- python 核心编程第5章(习题)
1.标准类型运算符. 写一段脚本,输入一个测验成绩,根据下面的标准,输出他的评分成绩(A-F). #coding:utf8 a = raw_input() a = int(a) if (a > ...
- 自我提升mysql
1.某字段更新 自增 1 update table set a=a+1 2.修改某一字段的数据类型 alter table "tablename" modify "co ...