1 Star 0 Fork 0

MetaverseMobile/leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
295_数据流的中位数_LTE.cpp 1.27 KB
一键复制 编辑 原始数据 按行查看 历史
class MedianFinder {
private:
// vector<int> counts(101, 0);
map<int, int> counts;
int nums = 0;
public:
/** initialize your data structure here. */
MedianFinder() {
}
void addNum(int num) {
/* 计数排序 */
counts[num]++;
nums++;
}
double findMedian() {
int cur = 0;
if (nums % 2) {
auto it = counts.begin();
int cur = it->second;
while (cur * 2 < nums) {
it++;
cur += it->second;
}
return it->first;
}
else {
auto it = counts.begin();
int cur = it->second;
while (cur * 2 < nums) {
it++;
cur += it->second;
}
auto next = it;
next++;
if (cur * 2 == nums)
return (next->first + it->first) / 2.0;
else {
return it->first;
}
}
return 0;
}
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/MetaverseMobile/leetcode.git
[email protected]:MetaverseMobile/leetcode.git
MetaverseMobile
leetcode
leetcode
main

搜索帮助