Leetcode 1630. Arithmetic Subarrays(易于理解的解法)

Leetcode 1630. Arithmetic Subarrays

题目链接: Arithmetic Subarrays

难度:Medium

题目大意:

根据题目要求构建子数组,判断子数组是否是Arithmetic数组。

思路:

写一个函数来判断一个数组是否是Arithmetic数组。然后按照题目要求依次构造子数组,进行判断即可。

代码

class Solution {
    public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
        int m=l.length;
        List<Boolean> res=new ArrayList<>();
        for(int i=0;i<m;i++){
            Boolean item=isArithmetic(Arrays.copyOfRange(nums,l[i],r[i]+1));
            res.add(item);
        }
        return res;
    }
    
    public Boolean isArithmetic(int[] nums){
        Arrays.sort(nums);
        int len=nums.length;
        int d=nums[1]-nums[0];
        for(int i=2;i<=len-1;i++){
            if(nums[i]-nums[i-1]!=d){
                return false;
            }
        }
        return true;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值