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, these are arithmetic sequences:
The following sequence is not arithmetic.
A zero-indexed array A consisting of N numbers is given. Asubsequenceslice of that array is any sequence of integers (P0, P1, ..., Pk) such that 0 ≤ P0< P1< ... < Pk< N.
Asubsequenceslice (P0, P1, ..., Pk) of array A is called arithmetic if the sequence A[P0], A[P1], ..., A[Pk-1], A[Pk] is arithmetic. In particular, this means that k ≥ 2.
The function should return the number of arithmetic subsequence slices in the array A.
The input contains N integers. Every integer is in the range of -231and 231-1 and 0 ≤ N ≤ 1000. The output is guaranteed to be less than 231-1.
Example:
分析
和ii的区别是这里数要连续不能打断,ii可以任意的sequence数组合,一旦打断cur = 0
这里是dict的数组,arr[i]=[dict*n] dict = {diff:count}
这里也是做2件事,一个update ans,就是ans+=f[j-1] 另一个就是Update f[j]+=1+f[j]
Last updated