LeetCode 1491. Average Salary Excluding the Minimum and Maximum Salary (去掉最低工资和最高工资后的工资平均值)

题目标签:Sort

  遍历array 的过程中,找出min 和 max, 最后用 sum - min - max 来求得平均数。

       具体看code。

 

Java Solution: 

Runtime:  0 ms, faster than 100.00% 

Memory Usage: 37.3 MB, less than 51.17 %

完成日期:9/7/2020

关键点:sum - min - max

class Solution {
    public double average(int[] salary) {
        double min = salary[0];
        double max = salary[0];
        double sum = 0;
        
        for(int s : salary) {
            min = Math.min(s, min);
            max = Math.max(s, max);
            sum += s;
        }
        
        return (sum - min - max) / (salary.length - 2);
    }
}

参考资料:n/a

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

posted @ 2020-09-08 05:50  Jimmy_Cheng  阅读(160)  评论(0编辑  收藏  举报