原題
給定一個未排序的整數(shù)數(shù)組,找到其中位數(shù)。
中位數(shù)是排序后數(shù)組的中間值,如果數(shù)組的個數(shù)是偶數(shù)個虑啤,則返回排序后數(shù)組的第N/2個數(shù)。
樣例
給出數(shù)組[4, 5, 1, 2, 3]架馋, 返回 3
給出數(shù)組[7, 9, 4, 5]狞山,返回 5
解題思路
- 排序,返回中位數(shù)叉寂。注意奇偶
完整代碼
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
return sorted(nums)[(len(nums) + 1) / 2 - 1]