1. Problem description
Given an integer array arr, please delete the smallest 5% of the numbers and the largest 5% of the numbers, the average of the remaining numbers.
Results within 10-5 of the standard answer were considered correct.
2. Problem-solving ideas
Sort first
and then traverse from the 5% of the array subscript to the 95% of the array subscript
3. Code implementation
class Solution {
public double trimMean(int[] arr) {
int len = arr.length;
int sum = 0;
Arrays.sort(arr);
for (int i = (int)(len * 0.05); i < (int)(len * 0.95); i++) {
sum += arr[i];
}
return sum / (len * 0.9);
}
}
4. Submit results
执行结果:通过
执行用时:2 ms, 在所有 Java 提交中击败了99.73%的用户
内存消耗:41 MB, 在所有 Java 提交中击败了83.38%的用户
通过测试用例:50 / 50