找到两个阵列之间的最小差异阵列、最小、差异、两个

2023-09-11 23:05:43 作者:撩妹被罚跪

由于两个排序阵列,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.