且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-09 17:33:56

由于数组排序,你可以通过它们与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.