题目链接 https://leetcode-cn.com/problems/find-the-distance-value-between-two-arrays/ 解题思路 暴力法,对arr1中的每个元素,判断Math.abs(arr1[i] - arr2[j]) <= d如果是那么对应的arr1[i]就不符合距离要求 AC代码 class Solution { public int findTheDistanceValue(int[] arr1, int[] arr2, int d) { int ans = 0; boolean tmp; for (int item : arr1) { tmp = true; for (int value : arr2) { if (Math.abs(item - value) <= d) { tmp = false; break; } } if (tmp) ans++; } return ans; } } 本地测试代码 package com.company; public class Solution_1385 { public static int findTheDistanceValue(int[] arr1, int[] arr2, int d) { int ans = 0; boolean tmp; for (int item : arr1) { tmp = true; for (int value : arr2) { if (Math.abs(item - value) <= d) { tmp = false; break; } } if (tmp) ans++; } return ans; } public static void main(String[] args) { //System.out.println(findTheDistanceValue(new int[]{4, 5, 8}, new int[]{10, 9, 1, 8}, 2)); System.out.println(findTheDistanceValue(new int[]{1, 4, 2, 3}, new int[]{-4, -3, 6, 10, 20, 30}, 3)); } }