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. 适配器 1、ArrayAdapter 2.SimpleAdapter

    1.ArrayAdapter(数组适配器):用于绑定格式单一的数据.数据源:可以是集合或数组 public class MainActivity extends AppCompatActivity { ...

  2. zencart产品批量表上传后SEO三要素状态以及特价时间修改

    ', metatags_products_name_status ', metatags_model_status ', metatags_price_status ', metatags_title ...

  3. URL编码以及GET和POST提交乱码解决方案 (转)

    1.  什么是URL编码. URL编码是一种浏览器用来打包表单输入的格式,浏览器从表单中获取所有的name和其对应的value,将他们以name/value编码方式作为URL的一部分或者分离的发送到服 ...

  4. Java运行环境绿色部署配置

    这个Java的绿色安装配置,还有从未自己的使用电脑说起来. 最近电脑运行慢,很长时间没有清理及维护了,而且有可能中毒或木马了,所以就把系统进行了Ghost还原了,所以原来安装的jdk环境也无法使用了, ...

  5. 关于Spring MVC写的不错的几篇博客

    关于Spring MVC写的不错的几篇博客 https://my.oschina.net/kolbe/blog/509810 https://www.cnblogs.com/sunniest/p/45 ...

  6. 给定一个矩阵 A, 返回 A 的转置矩阵。

    给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9]]输出:[[1,4,7], ...

  7. 虚拟dom比对原理

    dom对比步骤 1.用js对象来表达dom结构 tagName 标签名props 元素属性key 唯一标识children 子元素,格式和父元素一样count 有几个子元素,用于计算当前元素的索引,处 ...

  8. BBS-media配置

    media配置: 在上传头像的时候会用到media,首先需要在setting中加下面这一句话 MEDIA_ROOT=os.path.join(BASE_DIR,"blog",&qu ...

  9. nginx 访问控制模块

    截图,代码截屏均引用自慕课网nginx相关教学视频 基于用户的访问控制模块 http_access_module 基于用户登录信任的模块 http_access_module 参数示意:address ...

  10. 小程序上传base64的图片,可上传多张

    微信小程序上传图片转化为base64格式 clickimage: function(e) { var index = e.currentTarget.dataset.index; var count ...