lc 413 Arithmetic Slices


413 Arithmetic Slices

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.

规律公式 Accepted

这个问题就是找有几个长度大于等于3的等差数列,我们知道如果有一个等差数列长为n,可将其裂为(n-1)*(n-2)/2个长度大于等于3的子数列。

class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int len = 2, sum = 0;
for (int i = 2; i < A.size(); i++) {
if (A[i]+A[i-2] == 2*A[i-1]) {
len++;
} else {
if (len >= 3) sum += (len-1)*(len-2)/2;
len = 2;
}
}
if (len >= 3) sum += (len-1)*(len-2)/2;
return sum;
}
};

DP Accepted

如果用dp[i]表示到i位置为止的算数切片的个数,若能与之前两个数构成等差数列,则dp[i] = dp[i-1]+1,累加所有的dp[i]即可得到答案。

class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
vector<int> dp(A.size(), 0);
int ans = 0;
for (int i = 2; i < A.size(); i++) {
if (A[i]+A[i-2] == 2*A[i-1]) dp[i] = dp[i-1]+1;
ans += dp[i];
}
return ans;
}
};

LN : leetcode 413 Arithmetic Slices的更多相关文章

  1. LeetCode 413 Arithmetic Slices详解

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

  2. [LeetCode]413 Arithmetic Slices

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

  3. 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 ...

  4. Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力)

    Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力) 题目描述 如果一个数组1.至少三个元素2.两两之间差值相同,那么这个数组就是算术序列 比如下面的数组都是算 ...

  5. 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 ...

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

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

  7. 【Leetcode】413. Arithmetic Slices

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

  8. 413. Arithmetic Slices

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

  9. LeetCode 446. Arithmetic Slices II - Subsequence

    原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is ...

随机推荐

  1. Android——SlidingMenu学习总结

    来源 SlidingMenu是github上比較火开源库.很强大,不但但是简单的设置实现两側滑动菜单,还能够设置菜单的阴影.渐变色.划动模式等. 下载地址:https://github.com/jfe ...

  2. 怎样在 AutoLayout 中使用 UIScrollView (多个ContentView)

    http://codehappily.wordpress.com/2013/11/14/ios-how-to-use-uiscrollview-with-autolayout-pure-autolay ...

  3. 【iOS9系列】-3DTouch开发

    [iOS9系列]-3DTouch开发 第一:简介 3DTouch 是iOS9系统系统下,在iPhone6s(iPhone6s Plus)手机上才能够使用的功能.熟练使用后,发现还是很便捷的. 但是模拟 ...

  4. Boost中的Timer的使用——计算时间流逝

    使用Boost中的Timer库计算程序的运行时间 程序开发人员都会面临一个共同的问题,即写出高质量的代码完毕特定的功能.评价代码质量的一个重要标准就是算法的运行效率,也就是算法的运行时间.为了可靠的提 ...

  5. 双系统xp和ubuntu,删除ubuntu

    1:下载MbrFix.exe 2:进入c盘,命令MbrFix /drive 0 fixmbr

  6. 珠海鼎芯(D-Chip)IMX6读取CPU的UID的方法【转】

    本文转载自:http://blog.csdn.net/williamdedong/article/details/52712084 在使用IMX6板子的时候,有时会想着是否可以把板子搞一个唯一标识呢, ...

  7. YTU 2416: C语言习题 成绩输出

    2416: C语言习题 成绩输出 时间限制: 1 Sec  内存限制: 128 MB 提交: 1111  解决: 417 题目描述 输入n(<=10)个学生的姓名.学号和成绩,将其中不及格者的姓 ...

  8. Javascript 解析字符串生成 XML DOM 对象。

    Javascript 接收字符串生成 XML DOM 对象.实测对 Firefox .IE6 有效.可用于解析 ajax 的服务器响应结果,也可用于解析自定义字符串.​1. [代码]函数   ppt模 ...

  9. VS 一些用法设置

    /************************************************************************ * VS 一些用法设置 * 说明: * 最近要用到C ...

  10. SPOJ:Labyrinth(最大直线)

    The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is d ...