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. sql 批量触发器

    SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GO ALTER TRIGGER [dbo].[tr_insert_update_delete_sscode] O ...

  2. 重装系统失败后怎么用好系统U盘启动解决?

    很多朋友在第一次重装电脑系统时都会出现这样或那样的错误,导致重装系统后进不去系统,非常的被动.那么当我们遇到这种情况该怎么办呢?其实我们可以使用好系统U盘启动来解决重装系统后进不去的问题. 好系统U盘 ...

  3. 开源Android 恶意软件Radio Balouch

    安全研究机构 ESET 首次发现了开源 Android 间谍软件在 Google Play  上的恶意信息窃取行为,并且在被删除后仍在Google Play 重复出现.据悉,第一个间谍软件是基于开源间 ...

  4. Vi 入门简易教程

    首先,请注意,以下所讲的,全部是键盘在英文输入模式下.如果键盘是在中文输 入模式,全部的指令不正确. vi 有两种模式: Command Mode(指令模式) and Insert Mode(我姑且称 ...

  5. 原创:(一)TCP/IP学习笔记之概述

    端到端论点和命运共享其实不应该在底层,差错控制应该在应用程序附近来实现.这是因为考虑了连接,而不是传输的准确,因为差错可以根据某些算法(通信中的滤波等)来恢复,不过在大面积网络出现问题的时候有必要进行 ...

  6. Linux工具之ss

    1.SS命令 (Socket   Statistics),获取socket统计信息,显示和netstat类似的内容.显示更详细的TCP连接信息.   命令功能: ss(Socket Statistic ...

  7. Yii2 DatePicker时间选择器的使用

    bootstrap-Date Time Picker 详细配置请参考http://www.bootcss.com/p/bootstrap-datetimepicker/ use kartik\widg ...

  8. Linux rpm yum

    RPM : 1 rpm -q  子选项  软件名 -a :列出已安装所有的软件包 -i :查看指定软件的详细信息 -l:查看指定软件的文件安装清单 -f:查看某个目录.文件是哪个包带来的 rpm -q ...

  9. angular 中同级元素交替样式

    事件 : ng-click="addNews()"  所属div的层级:    div > div  >span 即,对于 event.target 查找的话最多 从s ...

  10. 牛客小白月赛12 D 月月给华华出题 (欧拉函数,数论,线筛)

    链接:https://ac.nowcoder.com/acm/contest/392/D 来源:牛客网 月月给华华出题 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 131072K, ...