【leetcode】1228.Missing Number In Arithmetic Progression
题目如下:
解题思路:题目很简单。先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失。
代码如下:
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]