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. ./configure && make && make install详解 (转)

    在Linux中利用源码包安装软件最重要的就是要仔细阅读安装包当中的README INSTALL两个说明文件,这两个文件会清楚的告诉你如何可以正确的完成这个软件的安装! 我们都知道源码包安装分为这么几个 ...

  2. [RK3288][Android6.0] 关于uboot中logo相关知识点小结【转】

    本文转载自:http://blog.csdn.net/kris_fei/article/details/76256224 Platform: Rockchip OS: Android 6.0 Kern ...

  3. Servlet启动运行顺序

    1.web项目执行属性 启动web项目后,web容器首先回去找web.xml文件,读取这个文件. 容器会创建一个 ServletContext ( servlet 上下文),整个 web 项目的所有部 ...

  4. bzoj 2815

    http://www.cnblogs.com/JS-Shining/archive/2013/01/12/2857429.html 题面 题解上写了用什么dominator tree,吓晕了,看了看, ...

  5. OC:数组排序、时间格式化字符串

    数组排序 //不可变数组的排序 NSArray * arr = [NSArray arrayWithObjects:@"hellow", @"lanou", @ ...

  6. Rails - ActiveRecord的where.not方法详解(copy)

    [说明:资料来自https://robots.thoughtbot.com/activerecords-wherenot] ActiveRecord's where.not Gabe Berke-Wi ...

  7. E20180225-hm-xa

    variation  n. 变化,变动; 变异,演变; 变奏曲; 变量; auxiliary adj. 辅助的 subscript  adj. 下标的,写在下方的,脚注的;  n. 下标,脚注,下角数 ...

  8. spoj 287 NETADMIN - Smart Network Administrator【二分+最大流】

    在spoj上用题号找题就已经是手动二分了吧 把1作为汇点,k个要入网的向t连流量为1的边,因为最小颜色数等于最大边流量,所以对于题目所给出的边(u,v),连接(u,v,c),二分一个流量c,根据最大流 ...

  9. 牛客网NOIP赛前集训营-提高组(第八场)

    染色 链接:https://ac.nowcoder.com/acm/contest/176/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言10 ...

  10. 一条SQL语句是如何执行的?--Mysql45讲笔记记录 打卡day1

    写在前面的话:回想以前上班的时候,空闲时间还是挺多的,但是都荒废了.如今找工作着实费劲了.但是这段时间在极客时间买了mysql45讲,就好像发现了新大陆一样,这是我认真做笔记的第一天,说实话第一讲我已 ...