classSolution { public: vector<double> sampleStats(vector<int>& count){ int n = count.size(); int total = accumulate(count.begin(), count.end(), 0); double mean = 0.0; double median = 0.0; int minnum = 256; int maxnum = 0; int mode = 0;
int left = (total + 1) / 2; int right = (total + 2) / 2; int cnt = 0; int maxfreq = 0; longlong sum = 0; for (int i = 0; i < n; i++) { sum += (longlong)count[i] * i; if (count[i] > maxfreq) { maxfreq = count[i]; mode = i; } if (count[i] > 0) { if (minnum == 256) { minnum = i; } maxnum = i; } if (cnt < right && cnt + count[i] >= right) { median += i; } if (cnt < left && cnt + count[i] >= left) { median += i; } cnt += count[i]; } mean = (double) sum / total; median = median / 2.0; return {(double)minnum, (double)maxnum, mean, median, (double)mode}; } };
classSolution: defsampleStats(self, count: List[int]) -> List[float]: n = len(count) total = sum(count) mean = 0.0 median = 0.0 min_num = 256 max_num = 0 mode = 0
left = (total + 1) // 2 right = (total + 2) // 2 cnt = 0 max_freq = 0 sum_ = 0 for i inrange(n): sum_ += count[i] * i if count[i] > max_freq: max_freq = count[i] mode = i if count[i] > 0: if min_num == 256: min_num = i max_num = i if cnt < right and cnt + count[i] >= right: median += i if cnt < left and cnt + count[i] >= left: median += i cnt += count[i] mean = sum_ / total median = median / 2.0 return [min_num, max_num, mean, median, mode]
funcsampleStats(count []int) []float64 { n := len(count) total := 0 for i := 0; i < n; i++ { total += count[i] } mean := 0.0 median := 0.0 minimum := 256 maxnum := 0 mode := 0
left := (total + 1) / 2 right := (total + 2) / 2 cnt := 0 maxfreq := 0 sum := 0 for i := 0; i < n; i++ { sum += int(count[i]) * int(i) if count[i] > maxfreq { maxfreq = count[i] mode = i } if count[i] > 0 { if minimum == 256 { minimum = i } maxnum = i } if cnt < right && cnt+count[i] >= right { median += float64(i) } if cnt < left && cnt+count[i] >= left { median += float64(i) } cnt += count[i] } mean = float64(sum) / float64(total) median = median / 2.0 return []float64{float64(minimum), float64(maxnum), mean, median, float64(mode)} }
var sampleStats = function(count) { let n = count.length; let total = count.reduce((acc, cur) => acc + cur, 0); let mean = 0.0; let median = 0.0; let min_num = 256; let max_num = 0; let mode = 0;
let left = parseInt((total + 1) / 2); let right = parseInt((total + 2) / 2); let cnt = 0; let maxfreq = 0; let sum = 0; for (let i = 0; i < n; i++) { sum += count[i] * i; if (count[i] > maxfreq) { maxfreq = count[i]; mode = i; } if (count[i] > 0) { if (min_num == 256) { min_num = i; } max_num = i; } if (cnt < right && cnt + count[i] >= right) { median += i; } if (cnt < left && cnt + count[i] >= left) { median += i; } cnt += count[i]; } mean = sum / total; median = median / 2.0; return [min_num, max_num, mean, median, mode]; }
publicclassSolution { publicdouble[] SampleStats(int[] count) { int n = count.Length; int total = count.Sum(); double mean = 0.0; double median = 0.0; int minnum = 256; int maxnum = 0; int mode = 0;
int left = (total + 1) / 2; int right = (total + 2) / 2; int cnt = 0; int maxfreq = 0; long sum = 0; for (int i = 0; i < n; i++) { sum += (long) count[i] * i; if (count[i] > maxfreq) { maxfreq = count[i]; mode = i; } if (count[i] > 0) { if (minnum == 256) { minnum = i; } maxnum = i; } if (cnt < right && cnt + count[i] >= right) { median += i; } if (cnt < left && cnt + count[i] >= left) { median += i; } cnt += count[i]; } mean = (double) sum / total; median = median / 2.0; returnnewdouble[]{minnum, maxnum, mean, median, mode}; } }
double* sampleStats(int* count, int countSize, int* returnSize) { int n = countSize; int total = 0; for (int i = 0; i < n; i++) { total += count[i]; } double mean = 0.0; double median = 0.0; int minnum = 256; int maxnum = 0; int mode = 0;
int left = (total + 1) / 2; int right = (total + 1) / 2 + ((total + 1) & 1); int cnt = 0; int maxfreq = 0; longlong sum = 0; for (int i = 0; i < n; i++) { sum += (longlong)count[i] * i; if (count[i] > maxfreq) { maxfreq = count[i]; mode = i; } if (count[i] > 0) { if (minnum == 256) { minnum = i; } maxnum = i; } if (cnt < right && cnt + count[i] >= right) { median += i; } if (cnt < left && cnt + count[i] >= left) { median += i; } cnt += count[i]; } mean = (double) sum / total; median = median / 2.0; double *ret = (double *)calloc(5, sizeof(double)); ret[0] = (double)minnum; ret[1] = (double)maxnum; ret[2] = mean; ret[3] = median; ret[4] = (double)mode; *returnSize = 5; return ret; }
复杂度分析
时间复杂度:O(n),其中 n 表示给定的数组的长度。根据题意可知道只需遍历数组两次,总体时间复杂度为 O(n)。