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. Java学习笔记【九、集合框架】

    集合框架设计的目标: 高性能. 允许不同类型的集合,以类似的方式工作,有互操作性. 对一个集合的扩展和适应必须简单. 集合框架包含: 接口:代表集合的抽象数据类型. 实现(类):具体实现(ArrayL ...

  2. 第十章· MySQL的主从复制

    一.主从复制简介  2015年5月28日11时,12小时后恢复,损失:平均每小时106.48W$ 1)高可用 2)辅助备份 3)分担负载 复制是 MySQL 的一项功能,允许服务器将更改从一个实例复 ...

  3. C# 列表中查找大小比较

    列表中查找大小比较

  4. 7.Nginx_Keepalived高可用配置

    2. 利用keepalived实现高可靠配置(HA) 2.1. 高可靠概念 HA(High Available):高可用性集群,是保证业务连续性的有效解决方案,一般有两个或两个以上的节点,且分为活动节 ...

  5. linux 计划任务超时控制和并发控制

    Linux crontab 的配置存放 /var/spool/cron 目录下. Linux crontab的日志文件存放再 /var/log/cron* timeout 命令 timeout 给脚本 ...

  6. manjaro开机出现grub 解决办法

    # 第一步:输入ls出现(hd0,msods1),(hd0,msdos5),(hd0,msods7)# 不同的电脑不一样,这是我电脑中的磁盘分区,和系统中的表示方法不一样,# linux中一般是/de ...

  7. 接口数据转json格式

    接口数据转json格式 function tojson($result, $callback = null){ header('Content-Type:text/html; charset=utf- ...

  8. Linux下DB2指令总结

    1.显示当前实例 >> get instance The current database manager instance is: db2axing 2.列出当前实例中激活的数据库 &g ...

  9. php类知识点滴---类的实例化,构造函数,继承初步

    实例化类----黑科技用法,通过字符串来实例化 class coach { public function __construct() { echo "欢迎光临北武堂"." ...

  10. POJ-3974-Palindrome(马拉车)

    链接: http://poj.org/problem?id=3974 题意: Andy the smart computer science student was attending an algo ...