python leetcode 462. Minimum Moves to Equal Array Elements II

  • Post author:
  • Post category:python


找到中位数即可

class Solution:
    def minMoves2(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        mid=len(nums)//2
        res=0
        for n in nums:
            res+=abs(n-nums[mid])
        return res



版权声明:本文为Neekity原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。