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.

Runtime: 4 ms, faster than 35.11% of C++ online submissions for Arithmetic Slices.

这一题的slice是连续的,所以只要判断i, i-1, i-2的关系然后递推就行了。

dp[i] = dp[i-1] + 1, 这里的dp[i]是以index i结尾的等差数列。 意思是,如果当前的位置能和之前两个位置组成等差数列,那么该位置上之前的的等差数列就等于

之前一个位置上结尾的(dp[i-1])数量(因为如果之前的位置上有相同差值的等差数列,再连接上以index i结尾的等差数列,还是可行的)然后再加上1,这个1

是以当前位置作为结尾,用前面的两个数合并起来的一个新的三个数的等差数列,这在之前的情况中是没有出现的。

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

LC 413. Arithmetic Slices的更多相关文章

  1. LN : leetcode 413 Arithmetic Slices

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

  2. Week 8 - 338.Counting Bits & 413. Arithmetic Slices

    338.Counting Bits - Medium Given a non negative integer number num. For every numbers i in the range ...

  3. LeetCode 413 Arithmetic Slices详解

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

  4. [LeetCode]413 Arithmetic Slices

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

  5. 413. Arithmetic Slices

    /**************************Sorry. We do not have enough accepted submissions.*********************** ...

  6. LeetCode - 413. Arithmetic Slices - 含中文题意解释 - O(n) - ( C++ ) - 解题报告

    1.题目大意 A sequence of number is called arithmetic if it consists of at least three elements and if th ...

  7. 【Leetcode】413. Arithmetic Slices

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

  8. LeetCoce 413. Arithmetic Slices

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

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

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

随机推荐

  1. web项目部署在centos 7验证码显示不出来解决方案

    今天把项目部署在centos7上,发现验证码显示不出来,看了一下tomcat日志 Exception in thread "http-nio-8080-exec-3" java.l ...

  2. ueditor 去掉自动跟随内容的<p><br /></p>

    //编辑器不能为空内容 if (domUtils.isEmptyNode(me.body)) { me.body.innerHTML = ''; ueditor.all.js  删除BR 如果还不行, ...

  3. window dos 下批量删除docker 容器

    >dokcer ps -a  -q> 1.txt (写入所有已暂停容器id) >for /f %a in (1.txt) do docker rm  %a for 循环     /f ...

  4. three.js之创建一个几何体

    <html> <head> <title>My first three.js app</title> <style> body { marg ...

  5. Hadoop_04_Hadoop 的HDFS客户端shell命令

    1.Hdfs shell客户端命令操作: 1.1.查看命令列表:hadoop fs 帮助如下: Usage: hadoop fs [generic options] [-appendToFile &l ...

  6. imx6ull增加qt5 qtserialbus库

    meta-qt5库地址:https://code.qt.io/cgit/yocto/meta-qt5.git/   1.在fsl-release-yocto/sources/meta-qt5/reci ...

  7. Python中json的简单读写操作

    Python中json的简单读写操作 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的 ...

  8. SpringData JPA 在解析实体类字段时驼峰自动添加下划线问题

    参考地址:https://my.oschina.net/javamaster/blog/2246886 SpringData JPA 使用的默认命名策略是: ImprovedNamingStrateg ...

  9. Hibernate的CRUD配置及简单使用

    参考博客:https://blog.csdn.net/qq_38977097/article/details/81326503 1.首先是jar包,可以在官网下载. 或者点击下面链接下载 链接:htt ...

  10. Pandas中DataFrame数据合并、连接(concat、merge、join)之join

    pandas.DataFrame.join 自己弄了很久,一看官网.感觉自己宛如智障.不要脸了,直接抄 DataFrame.join(other, on=None, how='left', lsuff ...