The Smallest Difference
Given two array of integers(the first array is array A, the second array is arrayB), now we are going to find a element in array A which is A[i], and another element in array B which is B[j], so that the difference between A[i] and B[j] (|A[i] - B[j]|) is as small as possible, return their smallest difference.
For example, given array A = [3,6,7,4], B = [2,8,9,3], return 0。
分析:
首先sort, 然后用两个指针分别指向两个array的头部,谁小移动谁。
public class Solution {
/**
* @param A, B: Two integer arrays.
* @return: Their smallest difference.
*/
public int smallestDifference(int[] A, int[] B) {
Arrays.sort(A);
Arrays.sort(B);
int pa = ;
int pb = ;
int diff = Integer.MAX_VALUE;
while (pa < A.length && pb < B.length) {
if (A[pa] < B[pb]) {
diff = Math.min(diff, Math.abs(A[pa] - B[pb]));
pa++;
} else if (A[pa] == B[pb]) {
return ;
} else {
diff = Math.min(diff, Math.abs(A[pa] - B[pb]));
pb++;
}
}
return diff;
}
}
The Smallest Difference的更多相关文章
- LintCode "The Smallest Difference"
Binary search. class Solution { int _findClosest(vector<int> &A, int v) { , e = A.size() - ...
- 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 ...
- Smallest Difference(暴力全排列)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10387 Accepted: 2 ...
- LintCode 387: Smallest Difference
LintCode 387: Smallest Difference 题目描述 给定两个整数数组(第一个是数组A,第二个是数组B),在数组A中取A[i],数组B中取B[j],A[i]和B[j]两者的差越 ...
- 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 ...
随机推荐
- 《Linux内核设计与实现》Chapter 2 读书笔记
<Linux内核设计与实现>Chapter 2 读书笔记 一.获取内核源码 1.使用Git 我们曾经在以前的学习中使用过Git方法 $ git clone git://git.kernel ...
- # linux读书笔记(3章)
linux读书笔记(3章) 标签(空格分隔): 20135328陈都 第三章 进程管理 3.1 进程 进程就是处于执行期的程序(目标码存放在某种存储介质上).但进程并不仅仅局限于一段可执行程序代码( ...
- 课堂讨论 alpha版最后总结
议时间:组队开发最后总结会议 星期一 下午4:30-5:30 会议地点:学院楼自习室 到会人员:唐野野 胡潘华 王永伟 魏孟 会议概要: 1.展示最后开发成果: 2.交流开发过程心得体会: 会 ...
- 第一Sprint阶段对各组提出的意见
组号 组名 意见 1 理财猫 1.界面深色的部分,字体应调成亮色,可以适当美化字体. 2.希望能够自动记录花销,而不是只能手动“记一笔”. 3.功能略少,比如可以添加“收入详情”.“支出详情”. 2 ...
- 睡眠猴子——beta阶段项目总结
Questions: 每个成员在beta 阶段的实践和alpha 阶段有何改进? 团队在beta 阶段吸取了那些alpha 阶段的经验教训? 12 条敏捷开发的原则中, 团队做得最好和最不好的各列 ...
- 弟三周作业之VS2015
这周有个任务就是安装VS2015,然后进行简单的单元测试. 首先我上吴小勇同学给的MSDN官网(http://www.itellyou.cn/)下载VS2013, 然后装上后,在工具栏里,点击工具里的 ...
- Beta阶段敏捷冲刺①
1.提供当天站立式会议照片一张. 每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 1.1昨天已完成的工作. 姓名 昨天已完成的工作 徐璐琳 熟悉"慧记" ...
- Windows server 自带的 .net版本
1. Win2012r2 所带的版本: 2. Win2016 所带的版本 4.6 Win2019 自带的 .net版本为: 4.7 4. 然后比较 Win2008r2sp1 使用的是 .net3.5 ...
- emoji & click copy
emoji & click copy document.execCommand("copy"); https://clipboardjs.com/ https://www. ...
- BZOJ2169 连边(动态规划)
令f[i][j]表示连i条边时奇点个数为j的方案数,转移时讨论两奇点相连.一奇一偶相连.两偶点相连即可.注意这样会造成重边,那么算出恰好有一条重边的方案数并减掉.由于是有序地考虑每条边,每次还要除以i ...