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.
这道题让我们算一种算数切片,说白了就是找等差数列,限定了等差数列的长度至少为3,那么[1,2,3,4]含有3个长度至少为3的算数切片,我们再来看[1,2,3,4,5]有多少个呢:

len = 3: [1,2,3], [2,3,4], [3,4,5]

len = 4: [1,2,3,4], [2,3,4,5]

len = 5: [1,2,3,4,5]

那么我们可以归纳出规律,长度为n的等差数列有1个,长度为n-1的等差数列有2个,... ,长度为3的等差数列有 n-2 个,那么总共就是 1 + 2 + 3 + ... + n-2 ,此时就要祭出高斯求和公式了,长度为n的等差数列中含有长度至少为3的算数切片的个数为(n-1)(n-2)/2,那么题目就变成了找原数组中等差数列的长度,然后带入公式去算个数即可,参见代码如下:

解法一:
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int res = , len = , n = A.size();
for (int i = ; i < n; ++i) {
if (A[i] - A[i - ] == A[i - ] - A[i - ]) {
++len;
} else {
if (len > ) res += (len - ) * (len - ) * 0.5;
len = ;
}
}
if (len > ) res += (len - ) * (len - ) * 0.5;
return res;
}
};

我们还可以用DP来做,定义一个一维dp数组,其中dp[i]表示,到i位置为止的算数切片的个数,那么我们从第三个数字开始遍历,如果当前数字和之前两个数字构成算数切片,那么我们更新dp[i]为dp[i-1]+1,然后res累加上dp[i]的值即可:

解法二:

class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int res = , n = A.size();
vector<int> dp(n, );
for (int i = ; i < n; ++i) {
if (A[i] - A[i - ] == A[i - ] - A[i - ]) {
dp[i] = dp[i - ] + ;
}
res += dp[i];
}
return res;
}
};

我们还可以进一步优化空间,用一个变量来代替上面的数组,原理都一样,参见代码如下:

解法三:

class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int res = , cur = ;
for (int i = ; i < A.size(); ++i) {
if (A[i] - A[i - ] == A[i - ] - A[i - ]) {
cur += ;
res += cur;
} else {
cur = ;
}
}
return res;
}
};

类似题目:

Arithmetic Slices II - Subsequence

参考资料:

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

https://leetcode.com/problems/arithmetic-slices/discuss/90058/simple-java-solution-9-lines-2ms

https://leetcode.com/problems/arithmetic-slices/discuss/90100/A-clear-python-solution-with-a-little-math

https://leetcode.com/problems/arithmetic-slices/discuss/90093/3ms-c-standard-dp-solution-with-very-detailed-explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Arithmetic Slices 算数切片的更多相关文章

  1. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  2. Leetcode: Arithmetic Slices

    A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...

  3. LeetCode——Arithmetic Slices

    Question A sequence of number is called arithmetic if it consists of at least three elements and if ...

  4. Leetcode: Arithmetic Slices II - Subsequence

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  5. LN : leetcode 413 Arithmetic Slices

    lc 413 Arithmetic Slices 413 Arithmetic Slices A sequence of number is called arithmetic if it consi ...

  6. LeetCode 446. Arithmetic Slices II - Subsequence

    原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is ...

  7. Leetcode之动态规划(DP)专题-413. 等差数列划分(Arithmetic Slices)

    Leetcode之动态规划(DP)专题-413. 等差数列划分(Arithmetic Slices) 如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列. 例如,以下数列为 ...

  8. LeetCode 413 Arithmetic Slices详解

    这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...

  9. 【LeetCode】413. Arithmetic Slices 等差数列划分

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 双指针 递归 动态规划 日期 题目地址:htt ...

随机推荐

  1. 跨域之jsonp

    我们都知道使用<script>标签可以引入外部的JS文件,即使这个JS文件来自于其他的网站,比如我们引用存放在网络服务器上的jQuery框架.在这个过程中,我们已经实现跨域访问.像< ...

  2. 3.EF 6.0 Code-First实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-entity-framework-5-0-code- ...

  3. Java内部类final语义实现

    本文描述在java内部类中,经常会引用外部类的变量信息.但是这些变量信息是如何传递给内部类的,在表面上并没有相应的线索.本文从字节码层描述在内部类中是如何实现这些语义的. 本地临时变量 基本类型 fi ...

  4. Servlet3.0的动态

    动态的创建是为了简化配置文件的.对于我们创建的servlet,filter和listener后可以使用.这也是注解的另外一种替代方式. 动态的添加有两种,一种是基于ContextListener的,另 ...

  5. Android 手机卫士8--删除通话记录

    1.编写代码需要注意bug: 再删除通话记录的时候,删除的是以前的通话记录,本次拦截下来的电话号码,通话记录没有删除?????? 问题原因:数据库中本次通话记录的电话号码还没有插入,就做了删除操作 2 ...

  6. 在js中为图片的src赋值时,src的值不能在开头用 破浪号~

    <img id="aa" src="~/Content/Manager/no01.png" /> document.getElementById(& ...

  7. UTFGrid

    UTFGrid UTFGrid is a specification for rasterized interaction data. As of version 1.2, it was remove ...

  8. swift学习笔记4——扩展、协议

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  9. java代码走查审查规范

    分类 重要性 检查项 备注 命名         重要 命名规则是否与所采用的规范保持一致? 成员变量,方法参数等需要使用首字母小写,其余单词首字母大写的命名方式,禁止使用下划线(_)数字等方式命名不 ...

  10. 如何写出高质量的技术博客 这边文章出自http://www.jianshu.com/p/ae9ab21a5730 觉得不错直接拿过来了 好东西要大家分享嘛

        如何写出高质量的技术博客?答案是:如果你想,就一定能写出高质量的技术博客.看起来很唯心,但这就是事实.有足够愿力去做一件目标明确,有良好反馈系统的事情往往很简单.就是不停地训练,慢慢地,你自己 ...