1. 程式人生 > 其它 >Java藍橋杯刷題記錄(二)

Java藍橋杯刷題記錄(二)

Python:

class MedianFinder:

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.stack=[]


    def addNum(self, num: int) -> None:
        self.stack.append(num) 
        def quick_sort(testlist,start,end):
            if start<end:
                i,j
=start,end base=testlist[i] while i<j: while i<j and testlist[j]>=base: j-=1 testlist[i],testlist[j]=testlist[j],testlist[i] while i<j and testlist[i]<=base: i
+=1 testlist[i],testlist[j]=testlist[j],testlist[i] testlist[i]=base quick_sort(testlist,start,i-1) quick_sort(testlist,i+1,end) quick_sort(self.stack,0,len(self.stack)-1) def findMedian(self) -> float: if not
self.stack: return None elif len(self.stack)%2==0: index=len(self.stack)//2 return (self.stack[index]+self.stack[index-1])/2.0 elif len(self.stack)%2!=0: index=len(self.stack)//2 return self.stack[index]