正常想到的就是按照逻辑返回,注意
“/”是浮点数除法,但是在此程序中需要整除,所以要用“%”或者“//”, 或者用"/"的话要加上int() 否则会报错:list indices must be integers or slices, not float
# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.nums = []
def Insert(self, num):
# write code here
self.nums.append(num)
def GetMedian(self):
# write code here
self.nums.sort()
n = len(self.nums)
if n%2 == 0:
return (self.nums[int(n/2)] + self.nums[int(n/2)-1])/2
if n%2 == 1:
return self.nums[int((n+1)/2)-1]
class Solution:
def __init__(self):
self.nums = []
def Insert(self, num):
# write code here
self.nums.append(num)
def GetMedian(self):
# write code here
self.nums.sort()
n = len(self.nums)
if n%2 == 0:
return (self.nums[int(n/2)] + self.nums[int(n/2)-1])/2
if n%2 == 1:
return self.nums[int((n+1)/2)-1]