classSolution { public: intnumOfSubarrays(vector<int>& arr, int k, int threshold){ int ans = 0; int s = 0; for (int i = 0; i < k; ++i) { s += arr[i]; } if (s >= threshold * k) { ++ans; } for (int i = k; i < arr.size(); ++i) { s = s + arr[i] - arr[i - k]; if (s >= threshold * k) { ++ans; } } return ans; } };
1344. Angle Between Hands of a Clock
计算钟表上时针和分针之间的角度。分别计算时针和分针的位置,然后算夹角即可。
时间复杂度: O(1),
空间复杂度: O(1).
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { public: doubleangleClock(int hour, int minutes){ double h = hour; double m = minutes; h = h * 5 + m / 12; double x = abs(h - m) * 6; if (x >= 180) { return360 - x; } else { return x; } } };