(Java) Leetcode 413. Arithmetic slices--arithmetic progression Division

Source: Internet
Author: User
Tags arithmetic diff

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

For example, these is arithmetic sequence:

1, 3, 5, 7, 97, 7, 7, 73,-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 was 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.

Thought for a long while this problem unexpectedly is a law problem ... Sometimes it's hard to fathom what problems need to be deduced and what the problem is to find the law.

The result is 1 if the array is {A-n-a};

If the array is {1,2,3,4}, the result is 3;

If the array is {1,2,3,4,5}, the result is 6;

If the array is {1,2,3,4,5,6}, the result is 10 ...

That is, if the newly-scanned element still retains a arithmetic progression relationship with the previous element, the number of newly formed sub-arithmetic progression will follow 1,2,3,4 ... The law increases. In fact, find the law of the problem to be made. So the point is to find the end of the difference sub-series, and at this time the result of the incremental update of the final result. When the arithmetic progression is not satisfied, the increment is reset to 1. See code below.

Java

classSolution { Public intNumberofarithmeticslices (int[] A) {if(A = =NULL|| A.length <= 2)return0; intCur = 1, res = 0, diff = a[1]-a[0];  for(inti = 2; i < a.length; i++) {            if(A[i]-a[i-1] = = diff) Res + = cur++; Else{diff= A[i]-a[i-1]; Cur= 1; }        }        returnRes; }   }

(Java) Leetcode 413. Arithmetic slices--arithmetic progression Division

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.