leetcode413

public class Solution {
    public int NumberOfArithmeticSlices(int[] A) {
        int curr = 0, sum = 0;
            for (int i = 2; i < A.Length; i++)
                if (A[i] - A[i - 1] == A[i - 1] - A[i - 2])
                {
                    curr += 1;
                    sum += curr;
                }
                else
                {
                    curr = 0;
                }
            return sum;
    }
}

https://leetcode.com/problems/arithmetic-slices/#/description

时间: 2024-10-08 00:52:48

leetcode413的相关文章

leetcode413 Aritmetic Slices Java

题目描述的不是太清楚. 1.A不一定是一个等差数列 2.等差数列必须在A中连续 也就是说如果A={1,3,5,6,8},那么返回结果就是1.{1,3,5} A={1,3,5,7,6,8},那么返回结果就是3.{1,3,5},{3,5,7},{1,3,5,7} 1 public int numberOfArithmeticSlices(int[] A) { 2 if(A.length <3) return 0; 3 int result = 0; 4 int curr = 0; 5 for(int

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