import math classSolution: deffindTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int: arr2.sort() legal_num = 0 for x in arr1: left = 0 right = len(arr2)-1 while left<=right: # 最后的left、right 就会停在 x 的左右两侧。 half = left+(right-left)//2 if arr2[half]==x: # 一旦找到直接相等的,就把左右索引改成自己的位置。 left = half right = half print("this time is ",left,right) break elif arr2[half] < x: left = half + 1 else: right = half - 1 print((left, right)) if left == len(arr2) or arr2[left]-x > d: # 左边出来了只比右边就行了,右边出来了,就只比左边就行了。左右两边都出来或者都没出来但是在范围内也行。 if right == -1or x-arr2[right] > d: legal_num += 1 print("legal_num is:", legal_num) return legal_num