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

数据流中的中位数

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

import java.util.Comparator;
import java.util.ArrayList;
public class Solution {
    ArrayList<Integer> list = new ArrayList<>();
    public void Insert(Integer num) {
        list.add(num);
        list.sort(Comparator.comparingInt(Integer::intValue));
    }

    public Double GetMedian() {
        int size = list.size();
        int mid = size / 2;
        if(size % 2 == 0){
            return (double)(list.get(mid-1) + list.get(mid)) / 2;
        }else{
            return (double)(list.get((size-1) / 2));
        }
    }


}
全部评论

相关推荐

艾莉Alliy:拼多多好像很看重笔试,我秋招的时候笔试ak了,面试问了好多八股,全都说“不会”,照样全部通过了面试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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