代码改变世界
[登录 · 注册]
  • leetcode 1093. Statistics from a Large Sample
  • 求一个数组的最小值,最大值,平均数,中位数与众数

    function sampleStats(count) {
                let ret = [], i, j
                //众数
                let most = -1;
                //中位数
                let medium = -1;
                //最小值
                let minimum = Infinity;
                //最大值
                let maximum = -Infinity;
                //平均值
                let average = -1;
                //出现次数最多的数出现了多少次
                let maxsum = -Infinity;
                //用于统计序列中数的个数
                let cnt = 0;
                //序列和 用于求平均值
                let sum = 0;
                for (i = 0; i < 256; i++) {
                    if (count[i] > 0) {
                        //找最大
                        if (i > maximum) {
                            maximum = i;
                        }
                        //找最小
                        if (i < minimum) {
                            minimum = i;
                        }
                        //为了求平均
                        sum += i * count[i];
                        cnt += count[i];
                        //找众数
                        if (count[i] > maxsum) {
                            most = i;
                            maxsum = count[i];
                        }
                    }
                }
    
                ret.push(minimum);
                ret.push(maximum);
                //求平均值
                average = sum / cnt;
                ret.push(average);
                //找中位数
                let nextcnt = 0;
                if (cnt % 2 == 1) {
                    //奇数
                    for (i = 0; i < 256; i++) {
                        if (count[i] > 0) {
                            if (nextcnt <= cnt / 2 && nextcnt + count[i] > cnt / 2) {
                                medium = i;
                                break;
                            }
                            nextcnt += count[i];
                        }
                    }
                } else {
                    //偶数
                    let pre = -1;
                    for (i = 0; i < 256; i++) {
                        if (count[i] > 0) {
                            if (nextcnt < cnt / 2 && nextcnt + count[i] > cnt / 2) {
                                medium = i;
                                break;
                            }
                            else if (nextcnt == cnt / 2 && nextcnt + count[i] > cnt / 2) {
                                medium = (i + pre) / 2.0;
                                break;
                            }
                            nextcnt += count[i];
                            pre = i;
                        }
                    }
                }
                ret.push(medium);
                ret.push(most);
    
                return ret;
            }
    
    

    更简洁的实现

    const sampleStats = function(count) {
      let min = Infinity;
      let max = -Infinity;
      let sum = 0;
      let totalCount = 0;
      let modCount = 0;
      let mod;
      
      for (let i = 0; i < count.length; i++) {
        if (!count[i]) continue;
        sum += i * count[i];
        totalCount += count[i];
        min = Math.min(min, i);
        max = Math.max(max, i);
        if (count[i] > modCount) {
          modCount = count[i];
          mod = i;
        }
      }
      
      const medIndex1 = Math.floor((totalCount - 1) / 2); 
      const medIndex2 = Math.ceil((totalCount - 1) / 2);
      
      let index = 0;
      let med1, med2;
      for (let i = 0; i < count.length; i++) {
        if (!count[i]) continue;
        index += count[i];
        if (med1 === undefined && medIndex1 < index) {
          med1 = i;
        }
        if (medIndex2 < index) {
          med2 = i;
          break;
        }
      }
      
      return [min, max, sum / totalCount, (med1 + med2) / 2, mod];
    };
    
  • 【推广】 阿里云小站-上云优惠聚集地(新老客户同享)更有每天限时秒杀!
    【推广】 云服务器低至0.95折 1核2G ECS云服务器8.1元/月
    【推广】 阿里云老用户升级四重礼遇享6.5折限时折扣!
  • 原文:https://www.cnblogs.com/rubylouvre/p/12151020.html
走看看 - 开发者的网上家园