Idea:Here we still use the method 'quicksort'
class Solution:
"""
@param nums: A list of integers
@return: An integer denotes the middle number of the array
"""
def median(self, nums):
# write your code here
def quicksort(nums, low, high):
pivot = get_pivot(nums, low, high)
if pivot == (len(nums)-1)//2:
return nums[pivot]
else:
if pivot > (len(nums)-1)//2:
return quicksort(nums, low, pivot-1)
else:
print(pivot)
return quicksort(nums, pivot+1, high)
def get_pivot(nums, low, high):
pivot = high
leftwall = low
for i in range(low, high):
if nums[pivot] >= nums[i]:
nums[leftwall], nums[i] = nums[i], nums[leftwall]
leftwall +=1
nums[leftwall], nums[high] = nums[high], nums[leftwall]
return leftwall
return quicksort(nums, 0, len(nums)-1)
网友评论