Skip to content

Commit c39ce07

Browse files
authored
Create Solution.java
1 parent 65480bb commit c39ce07

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
public double[] sampleStats(int[] count) {
3+
int n = count.length;
4+
int mode = 0, modeMax = 0;
5+
int min = -1, max = -1;
6+
double avg = 0;
7+
int cnt = 0;
8+
for (int i = 0; i < n; ++i) {
9+
if (count[i] > modeMax) {
10+
modeMax = count[i];
11+
mode = i;
12+
}
13+
if (count[i] != 0) {
14+
cnt += count[i];
15+
avg += count[i] * i;
16+
if (min == -1) min = i;
17+
max = i;
18+
}
19+
}
20+
avg /= cnt;
21+
// 求中位数
22+
double mid = 0;
23+
int sum = 0;
24+
for (int i = 0; i < n; ++i) {
25+
sum += count[i];
26+
if (sum << 1 > cnt) {
27+
mid = i;
28+
break;
29+
} else if (sum << 1 == cnt) {
30+
for (int j = i + 1; j < n; ++j) {
31+
if (count[j] != 0) {
32+
mid = (i + j) / 2.0;
33+
break;
34+
}
35+
}
36+
break;
37+
}
38+
}
39+
return new double[]{min, max, avg, mid, mode};
40+
}
41+
}

0 commit comments

Comments
 (0)