forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.2 KiB
58 lines
1.2 KiB
3 years ago
|
package main
|
||
|
|
||
|
/***
|
||
|
295. 数据流的中位数
|
||
|
中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。
|
||
|
|
||
|
例如:
|
||
|
[2,3,4] 的中位数是 3
|
||
|
[2,3] 的中位数是 (2 + 3) / 2 = 2.5
|
||
|
|
||
|
设计一个支持以下两种操作的数据结构:
|
||
|
void addNum(int num) - 从数据流中添加一个整数到数据结构中。
|
||
|
double findMedian() - 返回目前所有元素的中位数。
|
||
|
示例:
|
||
|
addNum(1)
|
||
|
addNum(2)
|
||
|
findMedian() -> 1.5
|
||
|
addNum(3)
|
||
|
findMedian() -> 2
|
||
|
进阶:
|
||
|
如果数据流中所有整数都在 0 到 100 范围内,你将如何优化你的算法?
|
||
|
如果数据流中 99% 的整数都在 0 到 100 范围内,你将如何优化你的算法?
|
||
|
*/
|
||
|
func main() {
|
||
|
|
||
|
}
|
||
|
|
||
|
type MedianFinder struct {
|
||
|
Nums []int
|
||
|
}
|
||
|
|
||
|
|
||
|
/** initialize your data structure here. */
|
||
|
func Constructor() MedianFinder {
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
func (this *MedianFinder) AddNum(num int) {
|
||
|
for i, j := 0, len(this.Nums); i < j; i++ {
|
||
|
if this.Nums[i] > num {
|
||
|
this.Nums[]
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
func (this *MedianFinder) FindMedian() float64 {
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Your MedianFinder object will be instantiated and called as such:
|
||
|
* obj := Constructor();
|
||
|
* obj.AddNum(num);
|
||
|
* param_2 := obj.FindMedian();
|
||
|
*/
|