1491. Average Salary Excluding the Minimum and Maximum Salary #
题目 #
- 给定整数数组
salary
,数组中每个数都是唯一的,其中salary[i]
是第i
个员工的工资。 - 返回去掉最低工资和最高工资后,剩下员工工资的平均值。
思路 #
模拟 #
代码 #
模拟 #
class Solution {
public double average(int[] salary) {
int minSalary = Integer.MAX_VALUE, maxSalary = Integer.MIN_VALUE, sum = 0;
for (int num: salary) {
minSalary = Math.min(minSalary, num);
maxSalary = Math.max(maxSalary, num);
sum += num;
}
return (double)(sum - minSalary - maxSalary) / (salary.length - 2);
}
}