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.

代码一

class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
if(A.size()<=2) return 0;
int cnt=0;
for(int i=0;i<=A.size()-3;i++){
int temp=A[i+1]-A[i];
for(int j=i+2;j<A.size();j++)
if(A[j]-A[j-1]==temp)
cnt++;
else
break;
}
return cnt;
}
};

代码二:动态规划

class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int n = A.size();
if (n < 3) return 0;
vector<int> dp(n, 0); // dp[i] means the number of arithmetic slices ending with A[i]
if (A[2]-A[1] == A[1]-A[0]) dp[2] = 1; // if the first three numbers are arithmetic or not
int result = dp[2];
for (int i = 3; i < n; ++i) {
// if A[i-2], A[i-1], A[i] are arithmetic, then the number of arithmetic slices ending with A[i] (dp[i])
// equals to:
// the number of arithmetic slices ending with A[i-1] (dp[i-1], all these arithmetic slices appending A[i] are also arithmetic)
// +
// A[i-2], A[i-1], A[i] (a brand new arithmetic slice)
// it is how dp[i] = dp[i-1] + 1 comes
if (A[i]-A[i-1] == A[i-1]-A[i-2])
dp[i] = dp[i-1] + 1;
result += dp[i]; // accumulate all valid slices
}
return result;
}
};

LeetCoce 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. LC 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. 继续(3n+1)猜想 (25)

    #include <algorithm> #include <iostream> using namespace std; int main(){ ] = { }; ], nu ...

  2. jsp问题记录

    2014-10-10 20:53:16 Jsp的el表达式:‘${value}’  用于获取后台传过来的值 而<%=value %>则是获取当前页面java代码的值

  3. 使用VMwaver 克隆CentOS 6.9网卡配置报错

    报错信息: 克隆完成之后,使用的是NAT模式,进入系统之后有IP地址也可以ping外网,但是没有ifcfg-eth0这个文件,使用setup命令配置网卡时报以下错误: 待解决-

  4. Thymeleaf 总结

    在javaScript中使用表达式 var list = /*[[${list}]]*/ null;   <script th:inline="javascript"> ...

  5. 访问TomCat出现的一些异常

    BUG-01:访问页面时出现: HTTP Status 500 servlet.init() for servlet DispatcherServlet threw exception...... 解 ...

  6. HttpMessageNotWritableException异常解决办法

    昨天做多对多的时遇到这个错误,网上找了一大堆,都没有解决掉,这个异常是说要解析的对象解析不了,就有可能该对象为null了,为了测试,我把数据库的数据都填上去    结果还是报错 看来是时候debug下 ...

  7. PetStore项目总结

    数据库(MySQL): account(用户表:没有外键), profile(用户侧面信息表:有两个外键:catid,username), category(宠物总分类表--鱼:没有外键), prod ...

  8. joomla多语言建站之默认前台语言设置

    joomla多语言建站后,如果想设置其中一种语言为默认前台语言,即: 从后台点击“Site Name”跳转时: 访问域名时: 页面自动切换至某一种语言,可以在后台通过“语言管理”模块来实现,将“网站前 ...

  9. 初次使用引用外部js心得

    在外部引用自己编辑的js时建立链接写在头部中是会出错的,如下图 错误如下: 这是一个是我初学时遇到的一个算是低级错误吧,看到这个错误,我以为的是我引用的js中编辑的代码是不是哪里写错了,但是看了好多遍 ...

  10. Long time no blogging

    It is a long time before I posted the last blog on myspace and seems that all of my blogs/documents ...