详见:https://leetcode.com/problems/sliding-window-median/description/
C++:
class Solution { public: vector<double> medianSlidingWindow(vector<int>& nums, int k) { vector<double> res; multiset<double> ms(nums.begin(), nums.begin() + k); auto mid = next(ms.begin(), k / 2); for (int i = k; ; ++i) { res.push_back((*mid + *prev(mid, 1 - k % 2)) / 2); if (i == nums.size()) { return res; } ms.insert(nums[i]); if (nums[i] < *mid) { --mid; } if (nums[i - k] <= *mid) { ++mid; } ms.erase(ms.lower_bound(nums[i - k])); } } };
参考:http://www.cnblogs.com/grandyang/p/6620334.html
来源:https://www.cnblogs.com/xidian2014/p/8903651.html