LeetCode 1502. Can Make Arithmetic Progression From Sequence (判断能否形成等差数列)

题目标签:Sort

  先排序,就很容易判定了,具体看code。

 

Java Solution: 

Runtime:  1 ms, faster than 98.60% 

Memory Usage: 38.9 MB, less than 92.87%

完成日期:8/11/2020

关键点:sort

class Solution {
    public boolean canMakeArithmeticProgression(int[] arr) {
        Arrays.sort(arr);
        
        int diff = arr[1] - arr[0];
        
        for(int i=1; i<arr.length-1; i++) {
            if(arr[i] + diff != arr[i+1]) {
                return false;
            }
        }
        
        return true;
    }
}

参考资料:n/a

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

上一篇:PAT 2021 春季 甲级 7-1 Arithmetic Progression of Primes


下一篇:分页查询