// y<= x <= 2y -14 publicintnumFriendRequests(int[] ages){ if (ages == null || ages.length <= 1) { return0; } int n = ages.length; Arrays.sort(ages); int left = 0; int right = 0; int res = 0; for (int i = 0; i < n; i++) { int age = ages[i]; //y<= x <= 2y -14 // 左边边界扩充 ,因y>=14 且 x>y,则age>=14 if (age < 15) { continue; } while (ages[left] <= 0.5 * age + 7) { left++; } //右侧边界扩充 while (right + 1 < n && ages[right + 1] <= age) { right++; } //因排序,当age往后移,则age越大,则边界也是在当前的基础上扩充。 //所以虽然两层循环依然时间是o(n) res += right - left; } return res ; } }