题目如下:
解题思路:题目很简单。先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失。
代码如下:
class Solution(object): def missingNumber(self, arr): """ :type arr: List[int] :rtype: int """ arr.sort() diff = (arr[-1] - arr[0])/(len(arr)) for i in range(len(arr)-1): if arr[i] + diff != arr[i+1]: return arr[i] + diff return arr[0]
原文地址:https://www.cnblogs.com/seyjs/p/11713478.html
时间: 2024-11-13 08:46:22