最小差
来源:互联网 发布:网络端口线连接 编辑:程序博客网 时间:2024/04/28 18:43
给定两个整数数组(第一个是数组 A
,第二个是数组 B
),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|)。返回最小差。
样例
给定数组 A = [3,4,6,7]
, B = [2,3,8,9]
,返回 0
。
挑战
时间复杂度 O(n log n)
class Solution {public: /** * @param A, B: Two integer arrays. * @return: Their smallest difference. */ int smallestDifference(vector<int> &A, vector<int> &B) { // write your code here int m = A.size(); int n = B.size(); sort(A.begin(), A.end()); sort(B.begin(), B.end()); int result = INT_MAX; int i = 0; int j = 0; while (i < m && j < n) { int temp = abs(A[i] - B[j]); if (temp < result) { result = temp; if (result == 0) { break; } } if (A[i] < B[j]) { i++; } else { j++; } } return result; }};
0 0
- 最小差
- 最小差
- 最小差
- 最小差
- 之间的差最小
- LintCode-最小差
- 006最小身高差
- LintCode:最小差
- LintCode_最小差
- LintCode:最小差
- HHU 1017: 最小差
- Java实现-最小差
- lintcode ---最小差
- Lintcode 最小差
- 最小差-LintCode
- 算法期中 最小差
- 序列和之差最小
- 数组之间的差最小
- Android-Animation动画(变换动画,帧动画,布局动画)
- yum 报错 cannot retrieve repository ……
- linux网络设置(有网卡别名介绍)
- 新人报道 欢迎大家分享交流指教
- 等宽编程字体Consoles YaHei
- 最小差
- MOS管的使用方法
- Android自定义控件--无限轮播Banner和Indicator实现
- Python介绍和安装
- 解决X64操作系统PL/SQL连接报错问题 make sure you have the 32 bits oracle client installed
- ubnkwguzerr
- 详解内网IP外网IP的关联及访问互联网原理
- Leet Code 17 Letter Combinations of a Phone Number
- 使用 apache-jmeter 完成WEB压力测试