Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 数据流中的中位数.py #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Target Offer/数据流中的中位数.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ def Insert(self, num):
self.right.append(num)
self.count += 1

def GetMedian(self, x):
def GetMedian(self):
if self.count == 1:
return self.left[0]
self.MaxHeap(self.left)
self.MinHeap(self.right)
if self.left[0] > self.right[0]:
while self.left[0] > self.right[0]:
self.left[0], self.right[0] = self.right[0], self.left[0]
self.MaxHeap(self.left)
self.MinHeap(self.right)
self.MaxHeap(self.left)
self.MinHeap(self.right)
if self.count & 1 == 0:
return (self.left[0] + self.right[0])/2.0
else:
Expand Down Expand Up @@ -66,3 +66,11 @@ def MinHeap(self, alist):
alist[k] = alist[index]
k = index
alist[k] = temp

s = Solution()
l = [4, 7, 0, 9, 1, 5, 3, 3, 2, 6]
s.MaxHeap(l)
print(l)
for i in range(1,10):
s.Insert(i)
s.GetMedian()