Problem
A sequence of numbers is called arithmetic if it consists of at least two elements, and the difference between every two consecutive elements is the same. More formally, a sequence s is arithmetic if and only if s[i+1] - s[i] == s[1] - s[0] for all valid i.
For example, these are arithmetic sequences:
1  | 1, 3, 5, 7, 9  | 
The following sequence is not arithmetic:
1  | 1, 1, 2, 5, 7  | 
You are given an array of n integers, nums, and two arrays of m integers each, l and r, representing the m range queries, where the ith query is the range [l[i], r[i]]. All the arrays are 0-indexed.
Return a list of boolean elements answer, where answer[i] is true if the subarray nums[l[i]], nums[l[i]+1], ... , nums[r[i]] can be rearranged to form an arithmetic sequence, and false otherwise.
Example 1:
1  | Input: nums = [4,6,5,9,3,7], l = [0,0,2], r = [2,3,5]  | 
Example 2:
1  | Input: nums = [-12,-9,-3,-12,-6,15,20,-25,-20,-15,-10], l = [0,1,6,4,8,7], r = [4,4,9,7,9,10]  | 
Constraints:
n == nums.lengthm == l.lengthm == r.length2 <= n <= 5001 <= m <= 5000 <= l[i] < r[i] < n-105 <= nums[i] <= 105
Analysis
  题目给出了一个数组nums[i],还有l和r,l[i]和r[i]组成一个区间,要求返回每一个区间是否是一个等差数列。所以这道题目的本质就是检查某个数组是否等差数列,这是一个通用的方法,在碰到其他的等差数列问题时也可以采取类似的方法。
  首先找出数列的最大值mx和最小值mi,如果两者相同说明里面的元素都是一样的,等差为0,所以也是等差数列。如果两者不相等,我们尝试计算等差diff = (mx - mi) / len,但是在计算前要检查能否被整除,如果不能被整除说明不存在等差。如果存在等差的话,还不能说明这就是一个等差数列,还要逐个数字检查,检查nums[i] - mi能否整除diff,同时相同的数字还不能出现两遍(等差为0的情况之前已经判断过了,如果这里存在两个数字相同,说明不符合要求)。如果所有的检查都没有问题的话,才能认为这是一个等差数列。
Solution
无。
Code
1  | class Solution {  | 
Summary
这道题目的重点就是怎么判断一个等差数列,我认为整理一下这个思路是很有用的。这道题目的分享到这里,感谢你的支持!