标签:
如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
    ArrayList<Integer> numList = null;
	Solution() {
		numList = new ArrayList<Integer>();
	}
	public void Insert(Integer num) {
	    numList.add(num);
    }
    public Double GetMedian() {
    	Collections.sort(numList);
    	int length = numList.size();
    	int temp = length / 2;
    	if (length % 2 == 0) {
    		return (double)(numList.get(temp) + numList.get(temp - 1)) / 2;
    	} else {
    		return (double)(numList.get(temp));
    	}
    }
}标签:
原文地址:http://www.cnblogs.com/rosending/p/5722458.html