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. 11gRAC报错CRS-4535, CRS-4000解决

    环境:AIX6.1 + Oracle11.2.0.4 RAC(2 nodes) 1.故障现象 2.定位问题 3.处理问题 1.故障现象 使用crsctl查看集群各资源状态,在任一节点都会直接报错CRS ...

  2. Spring Bean的生命周期(非常详细)

    Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...

  3. 用 jQuery.ajaxSetup 实现对请求和响应数据的过滤

    不知道同学们在做项目的过程中有没有相同的经历呢?在使用 ajax 的时候,需要对请求参数和响应数据进行过滤处理,比如你们觉得就让请求参数和响应信息就这么赤裸裸的在互联网里来回的穿梭,比如这样: 要知道 ...

  4. CSS 基础篇、绝对有你想要

    本章内容: 简介 CSS 定义 四种引入方式 样式应用的顺序 选择器(Selector) * 通用元素选择器 标签选择器 class 类选择器 # ID选择器 , 多元素选择器 后代元素选择器 > ...

  5. Android立体旋转动画实现与封装(支持以X、Y、Z三个轴为轴心旋转)

    本文主要介绍Android立体旋转动画,或者3D旋转,下图是我自己实现的一个界面 立体旋转分为以下三种: 1. 以X轴为轴心旋转 2. 以Y轴为轴心旋转 3. 以Z轴为轴心旋转--这种等价于andro ...

  6. 浅玩JavaScript的数据类型判断

    前言 平常在需要进行类型判断时,随手拿起typeof就像手枪一样只管突突突...也没有仔细的去了解它的具体特性. 所以这里就利用空闲时间,来做一个较为详细的了解. 首先我们来全面看一遍typeof类型 ...

  7. 手把手教从零开始在GitHub上使用Hexo搭建博客教程(三)-使用Travis自动部署Hexo(1)

    前言 前面两篇文章介绍了在github上使用hexo搭建博客的基本环境和hexo相关参数设置等. 基于目前,博客基本上是可以完美运行了. 但是,有一点是不太好,就是源码同步问题,如果在不同的电脑上写文 ...

  8. LINQ to SQL语句(18)之运算符转换

    运算符转换 1.AsEnumerable:将类型转换为泛型 IEnumerable 使用 AsEnumerable<TSource> 可返回类型化为泛型 IEnumerable 的参数.在 ...

  9. SSH框架和Redis的整合(1)

    一个已有的Struts+Spring+Hibernate项目,以前使用MySQL数据库,现在想把Redis也整合进去. 1. 相关Jar文件 下载并导入以下3个Jar文件: commons-pool2 ...

  10. 浅谈JDBC访问MySQL数据库

    经过我自己的总结后,其实很简单,只需要记住四个步骤,JDBC这部分的学习就可以掌握差不多了,请多多指教. 加载注册JDBC驱动: 打开数据库: 创建向数据库发送sql语句的statement: Res ...