-
Notifications
You must be signed in to change notification settings - Fork 0
/
zhaoshang2.py
49 lines (42 loc) · 1.28 KB
/
zhaoshang2.py
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
# -*- coding: utf-8 -*-
# @Date : 2019/4/9
# @Time : 21:01
# @Author : Daishijun
# @File : zhaoshang2.py
# Software : PyCharm
import heapq
class Solution:
def __init__(self):
self.maxlist = []
self.minlist = []
def Insert(self, num):
if (len(self.maxlist) + len(self.minlist)) &1 ==0: #偶数 ==>小堆
if len(self.maxlist)>0 and num < -self.maxlist[0]:
heapq.heappush(self.maxlist, -num)
num = -heapq.heappop(self.maxlist)
heapq.heappush(self.minlist, num)
else:
if len(self.minlist) >0 and num> self.minlist[0]:
heapq.heappush(self.minlist, num)
num = heapq.heappop(self.minlist)
heapq.heappush(self.maxlist, -num)
def GetMedian(self):
size = len(self.minlist) + len(self.maxlist)
if size == 0:
raise Exception('No nums')
median = 0
if size&1 :
median = self.minlist[0]
else:
median = (self.minlist[0] - self.maxlist[0])>>1
return median
if __name__ == '__main__':
# so = Solution()
# num = [2,3,4,2,6,2,5,1]
# size = 3
# print(so.maxInWindows(num, size))
a = 0 and 3
print(a)
print(type(a))
al = []
print(not al)