一般解法,面试必踢
class MedianFinder {
/** initialize your data structure here. */
List<Integer> list;
public MedianFinder() {
list = new ArrayList<>();
}
public void addNum(int num) {
list.add(num);
}
public double findMedian() {
double result = 0;
Collections.sort(list);
int t = list.size() / 2;
if(list.size() % 2 == 0) {
result = (list.get(t - 1) + list.get(t)) / 2.0;
} else {
result = list.get(t);
}
return result;
}
}
就是排个序找一下中位数
优先队列/堆
回家补
(参考博客)[https://blog.csdn.net/weixin_45830214/article/details/123708503]