加载中...
1502-判断能否形成等差数列(Can Make Arithmetic Progression From Sequence)
发表于:2021-12-03 | 分类: 简单
字数统计: 388 | 阅读时长: 1分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/can-make-arithmetic-progression-from-sequence

英文原文

A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.

Given an array of numbers arr, return true if the array can be rearranged to form an arithmetic progression. Otherwise, return false.

 

Example 1:

Input: arr = [3,5,1]
Output: true
Explanation: We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements.

Example 2:

Input: arr = [1,2,4]
Output: false
Explanation: There is no way to reorder the elements to obtain an arithmetic progression.

 

Constraints:

  • 2 <= arr.length <= 1000
  • -106 <= arr[i] <= 106

中文题目

给你一个数字数组 arr

如果一个数列中,任意相邻两项的差总等于同一个常数,那么这个数列就称为 等差数列

如果可以重新排列数组形成等差数列,请返回 true ;否则,返回 false

 

示例 1:

输入:arr = [3,5,1]
输出:true
解释:对数组重新排序得到 [1,3,5] 或者 [5,3,1] ,任意相邻两项的差分别为 2 或 -2 ,可以形成等差数列。

示例 2:

输入:arr = [1,2,4]
输出:false
解释:无法通过重新排序得到等差数列。

 

提示:

  • 2 <= arr.length <= 1000
  • -10^6 <= arr[i] <= 10^6

通过代码

高赞题解

[]
class Solution: def canMakeArithmeticProgression(self, arr: List[int]) -> bool: arr.sort() a=[arr[i+1]-arr[i] for i in range(len(arr)-1)] if len(set(a))==1: return True else: return False

image.png

统计信息

通过次数 提交次数 AC比率
26171 36548 71.6%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
1503-所有蚂蚁掉下来前的最后一刻(Last Moment Before All Ants Fall Out of a Plank)
下一篇:
1504-统计全 1 子矩形(Count Submatrices With All Ones)
本文目录
本文目录