题解 | #数据流中的中位数#

数据流中的中位数

https://www.nowcoder.com/practice/9be0172896bd43948f8a32fb954e1be1

import java.util.*;
public class Solution {

    List<Double> list = new ArrayList<>();
    public void Insert(Integer num) {
        list.add(Double.valueOf(num));
    }
    public Double GetMedian() {
        Collections.sort(list);
        if (list.size() % 2 == 0) {
            double n1 = list.get(list.size() / 2);
            double n2 = list.get((list.size() - 1) / 2);
            return (n1 + n2) / 2;
        } else {
            return list.get(list.size() / 2);
        }
    }
}

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务