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.

Example

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的更多相关文章

  1. LintCode "The Smallest Difference"

    Binary search. class Solution { int _findClosest(vector<int> &A, int v) { , e = A.size() - ...

  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. Smallest Difference(暴力全排列)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10387   Accepted: 2 ...

  5. LintCode 387: Smallest Difference

    LintCode 387: Smallest Difference 题目描述 给定两个整数数组(第一个是数组A,第二个是数组B),在数组A中取A[i],数组B中取B[j],A[i]和B[j]两者的差越 ...

  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. 101空降师506团2营E连全体成员

    吕天一 https://coding.net/u/Richardlv http://www.cnblogs.com/Richardlv/ 李伟亮 https://coding.net/u/201304 ...

  2. SCRUM站立会议模拟

    项目名称:连连看游戏 小组名称:计信F4 开会时间 :2016年10月11日 20:20~20:40 组长:张政 成员:张金生,武志远,李权 会议流程: 四个人模拟了一次的小组站立会议. 小组成员和组 ...

  3. PAT 甲级 1085 Perfect Sequence

    https://pintia.cn/problem-sets/994805342720868352/problems/994805381845336064 Given a sequence of po ...

  4. kubectl 命令记录 转帖自: https://www.kubernetes.org.cn/doc-45

    kubectl annotate – 更新资源的注解. kubectl api-versions – 以“组/版本”的格式输出服务端支持的API版本. kubectl apply – 通过文件名或控制 ...

  5. 2013长春网赛 1006 hdu 4764 Stone(巴什博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4764 题意:Tang 和 Jiang 玩一个游戏,轮流写下一个数,Tang先手,第一次Tang只能写[ ...

  6. java自定义注解学习(三)_注解解析及应用

    上篇文章已经介绍了注解的基本构成信息.这篇文章,主要介绍注解的解析.毕竟你只声明了注解,是没有用的.需要进行解析.主要就是利用反射机制在运行时进行查看和利用这些信息 常用方法汇总 在Class.Fie ...

  7. delphi执行查询语句时的进度条怎么做

    procedure TForm1.FormCreate(Sender: TObject);  begin     ADOQuery1.ExecuteOptions := [eoAsyncFetch]; ...

  8. Easy-UI开发总结

    Easy-UI开发总结 jQuery EasyUI 简介 jQuery EasyUI 是一个基于 jQuery 的框架,集成了各种用户界面插件. 什么是 jQuery EasyUI jQuery Ea ...

  9. 前端开发【第6篇:JavaScript客户端(浏览器)】

    Web浏览器中的JavaScript 客户端JavaScript时间线 1.Web浏览器创建Document对象,并且开始解析web页面,解析HTML元素和它门的文本内容后添加Element对象和Te ...

  10. 分布式为什么使用Redis

    一 为什么使用 Redis 在项目中使用 Redis,主要考虑两个角度:性能和并发.如果只是为了分布式锁这些其他功能,还有其他中间件 Zookpeer 等代替,并非一定要使用 Redis. 性能: 如 ...