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 ...
随机推荐
- 机器学习技法-神经网络(NNet)
课程地址:https://class.coursera.org/ntumltwo-002 重要!重要!重要~ 一.神经网络(NNet)的动机 神经网络有很久的历史,由感知机(perceptron)模型 ...
- php中读取文件内容的几种方法
1.fread string fread ( int $handle , int $length ) fread() 从 handle 指向的文件中读取最多 length 个字节.该函数在读取完最多 ...
- my Style
1. box-sizing语法: box-sizing : content-box || border-box || inherit 参数取值: content-box:此值为其默认值,其让元素维持W ...
- leetcode 99 Recover Binary Search Tree ----- java
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- spark共享变量
boradcast例子代码: scala版本 spark共享变量之Accumulator 例子代码: scala版本
- Mac上因磁盘格式导致gulp无限刷新问题
今天遇到个超奇葩的问题,使用gulp.watch监控文件变化,但是并没有修改文件,却一直执行change,导致浏览器无限刷新 调试了10小时,代码各种删改,一直不得其解.切换到Windows运行,又正 ...
- 黑马程序员——JAVA基础之简述 类的封装
------- android培训.java培训.期待与您交流! ---------- 类的封装(Encapsulation) 封装:是指隐藏对象的属性和实现细节,仅对外提供公共访问方式. 封装优 ...
- Hadoop 安装记录
第一步:打开/etc 下面的 profile文件,在其中加入环境变量设置的代码 done JAVA_HOME=/home/hadoop/installer/jdk7u65 PATH=$JAVA_HOM ...
- windows7旗舰版激活密钥永久版免费分享
windows7之家不仅提供精品Win7教程 给大家,加上这个windows7激活密匙还帮大家解决windows7系统激活问题,包括win7旗舰版 windows7安装版这些. 用的是Windows7 ...
- Freezing Your Tuples Off 之 vacuum_freeze_min_age
The vacuum_freeze_min_age setting determines the youngest XID which will be changed to FrozenXID on ...