找到两个阵列之间的最小差异 [英] Find the minimum difference between two arrays

查看:191
本文介绍了找到两个阵列之间的最小差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于两个排序阵列,A和B,发现I,J为其中| A [1] - B [J]。|最小。

Given two sorted arrays, A and B, find i,j for which |A[i] - B[j]| is minimum.

推荐答案

由于数组排序,你可以通过它们与2指针(每个阵列)。如果 | A [1 + 1] - B [J]。| < | A [1] - B [J + 1] | 然后增加,否则增加Ĵ。继续,直到你已经达到了其中一个数组的结束。保持最低限度指标的跟踪,当您去。

Since the arrays are sorted, you can pass through them with 2 pointers (one for each array). If |A[i+1] - B[j]| < |A[i] - B[j+1]| then increment i, otherwise increment j. Continue until you've reached the end of one of the arrays. Keep track of the minimal indexes as you go.

这篇关于找到两个阵列之间的最小差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆