[코테 풀이] Arithmetic Subarrays

시내·2024년 6월 27일

Q_1630) Arithmetic Subarrays

출처 : https://leetcode.com/problems/arithmetic-subarrays/

class Solution {
    public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
        List<Boolean> arrayList = new ArrayList<>();
        List<Integer> sub = new ArrayList<>();
        for (int i = 0; i < l.length; i++) {
            boolean flag = true;
            for (int n = l[i]; n <= r[i]; n++) {
                sub.add(nums[n]);
            }
            Collections.sort(sub);
            for (int m = 0; m < sub.size() - 1; m++) {
                if ((sub.get(m + 1) - sub.get(m)) != (sub.get(1) - sub.get(0))) {
                    arrayList.add(false);
                    flag = false;
                    break;
                }
            }
            if (flag) arrayList.add(true);
            sub.clear();
        }

        return arrayList;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글