数据流中的中位数

题目


分析
数据流中的中位数

代码

import java.util.*;
public class Solution {

    ArrayList<Integer> maxHeap=new ArrayList<>();
    ArrayList<Integer> minHeap=new ArrayList<>();
    private int total=0;
    public void Insert(Integer num) {
        if(total%2==0){//当大根堆和小根堆中所有数据和是偶数时插入小根堆
            if(maxHeap.size()==0){
                minHeap.add(num);
            }else {
                int max = maxHeap.get(0);//获取大根堆中的最大数值
                if(max<=num){//插入数值比大根堆最大数值大直接插入小根堆
                    minHeap.add(num);
                    //重新构建小根堆
                    BuildeMinHeap();
                }else {//插入数值比大根堆最大数值小,先移除最大堆的最大值,将其插入最小堆,再将num插入最大堆
                    Integer temp=maxHeap.get(0);
                    minHeap.add(temp);
                    maxHeap.set(0,num);
                    //重新构建大根堆与小根堆
                    BuildeMinHeap();
                    BuildMaxHeap();
                }
            }
        }else {//当大根堆和小根堆中所有数据和是奇数时插入大根堆
            int min = minHeap.get(0);//获取小根堆中的最小数值
            if(min>=num){//插入数值比小根堆最小数值小时,直接插入大根堆
                maxHeap.add(num);
                //重新构建大根堆
                BuildMaxHeap();
            }else {//插入数值比小根堆最小数值大时,先移除小根堆最小数值,插入大根堆,再将num插入小根堆
                Integer temp=minHeap.get(0);
                minHeap.set(0,num);
                maxHeap.add(temp);
                //重新构建大根堆与小根堆
                BuildeMinHeap();
                BuildMaxHeap();
            }
        }
        total++;
    }

    public Double GetMedian() {
        if(total%2==0){
            return (maxHeap.get(0)+minHeap.get(0))/2.0;
        }else {
            return minHeap.get(0)/1.0;
        }
    }
    //构建大根堆
    public void BuildMaxHeap(){
        for(int i=(maxHeap.size()-1)/2;i>=0;i--){
            int nowIndex=i;
            while (2*nowIndex+1<maxHeap.size()){
                int bigIndex=2*nowIndex+1;
                if(bigIndex+1<maxHeap.size()){
                    if(maxHeap.get(bigIndex)<maxHeap.get(bigIndex+1)){
                        bigIndex+=1;
                    }
                }
                if(maxHeap.get(nowIndex)<maxHeap.get(bigIndex)){
                    Integer temp=maxHeap.get(bigIndex);
                    maxHeap.set(bigIndex,maxHeap.get(nowIndex));
                    maxHeap.set(nowIndex,temp);
                    nowIndex=bigIndex;
                }else {
                    break;
                }
            }
        }
    }
    //构建小根堆
    public void BuildeMinHeap(){
        for(int i=(minHeap.size()-1)/2;i>=0;i--){
            int nowIndex=i;
            while (2*nowIndex+1<minHeap.size()){
                int smallIndex=2*nowIndex+1;
                if(smallIndex+1<minHeap.size()){
                    if(minHeap.get(smallIndex)>minHeap.get(smallIndex+1)){
                        smallIndex+=1;
                    }
                }
                if(minHeap.get(nowIndex)>minHeap.get(smallIndex)){
                    Integer temp=minHeap.get(smallIndex);
                    minHeap.set(smallIndex,minHeap.get(nowIndex));
                    minHeap.set(nowIndex,temp);
                    nowIndex=smallIndex;
                }else {
                    break;
                }
            }
        }
    }

}
posted @ 2018-04-03 15:19  baixiaoshuai  阅读(170)  评论(0编辑  收藏  举报