题目描述:
我们对 0 到 255 之间的整数进行采样,并将结果存储在数组 count 中:count[k] 就是整数 k 的采样个数。
我们以 浮点数 数组的形式,分别返回样本的最小值、最大值、平均值、中位数和众数。其中,众数是保证唯一的。
我们先来回顾一下中位数的知识:
如果样本中的元素有序,并且元素数量为奇数时,中位数为最中间的那个元素;
如果样本中的元素有序,并且元素数量为偶数时,中位数为中间的两个元素的平均值。
示例 1:
输入:count = [0,1,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
输出:[1.00000,3.00000,2.37500,2.50000,3.00000]
示例 2:
输入:count = [0,4,3,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
输出:[1.00000,4.00000,2.18182,2.00000,1.00000]
提示:
count.length == 256
1 <= sum(count) <= 10^9
计数表示的众数是唯一的
答案与真实值误差在 10^-5 以内就会被视为正确答案
思路:
题目的意思就是count[index] = k,表示出现了k个index,这样就明白了
中位值的计算可能需要一些技巧
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
class Solution {
public double[] sampleStats(int[] count) {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
double sum = 0;
double []result = new double[5];
int index = 0;
int size = 0;
int zhongshu = 0;
int time = 0;
for (int i = 0; i < count.length; i++) {
if(count[i] != 0){
min = i;
break;
}
}
Map<Integer, Integer> getMap = new TreeMap<>();
for (int i = count.length - 1; i >= 0; i--) {
if(count[i] != 0){
max = i;
break;
}
}
for (int d : count) {
if(time < d){
time = d;
zhongshu = index;
}
sum += d * index;
if(d != 0){
getMap.put(index, d);
}
index ++;
size += d;
}
result[0] = min;
result[1] = max;
result[2] = (double)(sum / (size * 1.0));
int mid = 0;
int num1 = 0;
int num2 = 0;
boolean flag = true;
for (Entry<Integer, Integer> integer : getMap.entrySet()) {
mid += integer.getValue();
if(mid >= size / 2 && flag){
num1 = integer.getKey();
flag = false;
}
if(mid >= size / 2 + 1){
num2 = integer.getKey();
break;
}
}
if(size % 2 == 1){
result[3] = num1;
}else {
result[3] = (num1 + num2) / 2.0;
}
result[4] = zhongshu;
return result;
}
}
代码写的比较复杂了,取最大和最小值我是分开了
这个代码就显得比较简单了
class Solution {
public double[] sampleStats(int[] count) {
double[] result = new double[5];
int minV = -1,maxV=-1,ct = 0,maxC=-1;
long sum = 0;
for(int i=0;i<count.length;++i){
if(count[i]!=0){
maxV=i;
if(minV==-1)
minV=i;
if(maxC==-1||count[i]>count[maxC])
maxC=i;
}
sum+=i*count[i];
ct+=count[i];
}
int findCt = (ct+1)/2;
int temp = 0;
int ctV = 0;
for(int i=0;i<count.length;++i){
if(count[i]>0) {
findCt -= count[i];
if (findCt <= 0) {
ctV += i;
break;
}
}
}
findCt = (ct+1)/2;
for(int i=count.length-1;i>=0;--i){
if(count[i]>0) {
findCt -= count[i];
if (findCt <= 0) {
ctV += i;
break;
}
}
}
result[0] = (double)minV;
result[1] = (double)maxV;
result[2] = (double)sum;
result[3] = (double)ctV;
result[4] = (double) maxC;
result[2]/=ct;
result[3]/=2;
return result;
}
}