LeetCode1491-去掉最低工资和最高工资后的工资平均值

LeetCode1491-去掉最低工资和最高工资后的工资平均值

循环去头尾相加取平均

1
2
3
4
5
6
7
8
9
10
public double average01(int[] salary) {
Arrays.sort(salary);
int sum = 0;
int len = salary.length - 1;
for (int i = 1; i < len; i++) {
sum += salary[i];
}
return 1.0 * sum / (salary.length - 2);

}

通过Math函数

1
2
3
4
5
6
7
8
9
10
11
public double average02(int[] salary) {
double sum = 0;
double maxValue = Integer.MIN_VALUE;
double minValue = Integer.MAX_VALUE;
for (int num : salary) {
sum += num;
maxValue = Math.max(maxValue, num);
minValue = Math.min(minValue, num);
}
return (sum - maxValue - minValue) / (salary.length - 2);
}