-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetCode0295.java
81 lines (69 loc) · 2.32 KB
/
LeetCode0295.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Find Median from Data Stream
* addNum(1)
* addNum(2)
* findMedian() -> 1.5
* addNum(3)
* findMedian() -> 2
* */
import java.util.ArrayList;
import java.util.Collections;
import java.util.PriorityQueue;
public class LeetCode0295 {
public static void main(String args[]) {
MedianFinder median = new MedianFinder();
median.addNum(2);
median.addNum(1);
System.out.println(median.findMedian());
median.addNum(3);
System.out.println(median.findMedian());
}
}
class MedianFinder {
private int count;
//注释的部分为利用排序寻找中位数,时间复杂度为O(NlogN)
//private ArrayList<Integer> nums;
private PriorityQueue<Integer> maxheap;
private PriorityQueue<Integer> minheap;
/**
* initialize your data structure here.
*/
public MedianFinder() {
/*count = 0;
nums = new ArrayList<>();*/
count = 0;
maxheap = new PriorityQueue<>((x, y) -> y - x);
minheap = new PriorityQueue<>();
}
public void addNum(int num) {
/*count += 1;
nums.add(num);
Collections.sort(nums);
System.out.println(nums);*/
count += 1;
maxheap.offer(num);
minheap.add(maxheap.poll());
// 如果两个堆合起来的元素个数是奇数,小顶堆要拿出堆顶元素给大顶堆
if ((count & 1) != 0)
maxheap.add(minheap.poll());
}
public double findMedian() {
/*if (count % 2 == 0) {
double left = nums.get(count / 2 - 1);
double right = nums.get(count / 2);
return (left + right) / 2;
} else
return nums.get(count / 2);*/
if (count % 2 == 0)
// 如果两个堆合起来的元素个数是偶数,数据流的中位数就是各自堆顶元素的平均值
return (double) (maxheap.peek() + minheap.peek()) / 2;
else
// 如果两个堆合起来的元素个数是奇数,数据流的中位数大顶堆的堆顶元素
return (double) maxheap.peek();
}
}
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/