(Java) LeetCode 413. Arithmetic Slices —— 等差数列划分

A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.

For example, these are arithmetic sequence:

1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9

The following sequence is not arithmetic.

1, 1, 2, 5, 7

A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 <= P < Q < N.

A slice (P, Q) of array A is called arithmetic if the sequence:
A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q.

The function should return the number of arithmetic slices in the array A.

Example:

A = [1, 2, 3, 4]
return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.


想了半天这道题竟然是一道找规律问题…有的时候真的难以捉摸什么问题需要推导,什么问题是找规律。

如果数组是{1,2,3},结果是1;

如果数组是{1,2,3,4},结果是3;

如果数组是{1,2,3,4,5},结果是6;

如果是数组是{1,2,3,4,5,6},结果是10……

也就是说如果新扫描到的元素仍能和之前的元素保持等差数列关系,那么新形成的子等差数列个数按照1,2,3,4……的规律增加。其实找到规律这道题就做出来了。所以本题根本是寻找差子数列的终点,并以此时的结果增量更新最终结果。不满足等差数列的时候增量就要重置为1。见下文代码。



Java

class Solution {
    public int numberOfArithmeticSlices(int[] A) {
        if (A == null || A.length <= 2) return 0;
        int cur = 1, res = 0, diff = A[1] - A[0];
        for (int i = 2; i < A.length; i++) {
            if (A[i] - A[i-1] == diff) res += cur++;
            else {
                diff = A[i] - A[i-1];
                cur = 1;
            }
        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/tengdai/p/9300899.html

时间: 2024-10-17 13:18:33

(Java) LeetCode 413. Arithmetic Slices —— 等差数列划分的相关文章

413 Arithmetic Slices 等差数列划分

如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列.例如,以下数列为等差数列:1, 3, 5, 7, 97, 7, 7, 73, -1, -5, -9以下数列不是等差数列.1, 1, 2, 5, 7数组 A 包含 N 个数,且索引从0开始.数组 A 的一个子数组划分为数组 (P, Q),P 与 Q 是整数且满足 0<=P<Q<N .如果满足以下条件,则称子数组(P, Q)为等差数组:元素 A[P], A[p + 1], ..., A[Q - 1], A[Q]

LeetCode - 413. Arithmetic Slices - 含中文题意解释 - O(n) - ( C++ ) - 解题报告

1.题目大意 A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The

Leetcode-413 Arithmetic Slices(等差数列划分)

1 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 2 class Solution 3 { 4 public: 5 int f(int len) 6 { 7 int sum = 0; 8 len -= 2; 9 _for(i,1,len+1) 10 sum += i; 11 return sum; 12 } 13 int numberOfArithmeticSlices(vector<int>& A) 14 { 15 int

Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力)

Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力) 题目描述 如果一个数组1.至少三个元素2.两两之间差值相同,那么这个数组就是算术序列 比如下面的数组都是算术序列: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 但是这一个就不是: 1, 1, 2, 5, 7 求给定数组,能有多少个算术序列 测试样例 Input: [1, 2, 3, 4] Output: 3 有三个算术序列切片: [1,2,3], [2,3,4], [1

413. Arithmetic Slices(LeetCode)

A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The followi

【Leetcode】413. Arithmetic Slices

Description A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9

LeetCode 446. Arithmetic Slices II - Subsequence

原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, thes

413. Arithmetic Slices

A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The followi

413. Arithmetic Slices(数组中等差递增子区间的个数)

A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The followi