Question

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.

Solution

动态规划,先计算相邻之间的差值,然后遍历,每增加一个相同的差值,那么总数就加上之前相同差值的个数。

举个例子:

[1, 2, 3, 4], 差值数组 table = [1, 1, 1],遍历差值数组的时候,遍历到table[1]的时候,之前有一个1,那么总数加1,遍历到table[2]的时候,总数加2,因此总数求和等于1+2,因此有3组。

Code

class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int count = 0;
for (int i = 1; i < A.size(); i++) {
table.push_back(A[i] - A[i - 1]);
}
if (table.size() < 2)
return 0; int prev = table[0];
int tmp = 1;
for (int i = 1; i < table.size(); i++) {
if (table[i] == prev) {
count += tmp;
tmp++;
} else
tmp = 1;
prev = table[i];
} return count;
}
private:
vector<int> table;
};

LeetCode——Arithmetic Slices的更多相关文章

  1. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

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

  2. [LeetCode] Arithmetic Slices 算数切片

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

  3. Leetcode: Arithmetic Slices

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

  4. Leetcode: Arithmetic Slices II - Subsequence

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

  5. LeetCode 446. Arithmetic Slices II - Subsequence

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

  6. Leetcode之动态规划(DP)专题-413. 等差数列划分(Arithmetic Slices)

    Leetcode之动态规划(DP)专题-413. 等差数列划分(Arithmetic Slices) 如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列. 例如,以下数列为 ...

  7. LeetCode 413 Arithmetic Slices详解

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

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

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

  9. [LeetCode]413 Arithmetic Slices

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

随机推荐

  1. python学习笔记(四)— 补充

    函数return多个值 函数如果有多个return值,那么会生成一个元组里面 def hello(a,b,c,d): return a,b,c,d res =hello('aa','cc','dd', ...

  2. LINUX内核分析20133201

    实验:通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的 学号:20133201 姓名:李冬辉 注: 原创作品转载请注明出处 +<Linux内核分析>MOOC课程http://m ...

  3. 解决hung_task_timeout_secs问题【方法待校验】

    问题描述:   kernel: "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this messag ...

  4. 小技巧-如何加快github下载代码的速度(转)

    作为开发人员,github是大家的标配了,常常会苦恼于gitclone某个项目的时候速度太慢,看着控制台那几K十几K的龟速,吐血!! 原因很简单:github的CDN被伟大的墙屏蔽所致. 所以解决方案 ...

  5. GROUP BY 和 ORDER BY一起使用

    转:http://lzfhope.blog.163.com/blog/static/636399220092554045196/ 环境:oracle 10g单单group by 或者order by本 ...

  6. PAT 1130 Infix Expression[难][dfs]

    1130 Infix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspond ...

  7. 8种主要排序算法的C#实现 (一)

    简介 排序算法是我们编程中遇到的最多的算法.目前主流的算法有8种. 平均时间复杂度从高到低依次是: 冒泡排序(o(n2)),选择排序(o(n2)),插入排序(o(n2)),堆排序(o(nlogn)), ...

  8. python16_day21【git and celery】

    一.Git使用 ## GIT常用执令说明 * 初始化git项目 > git init * 查看当前目录状态 > git status * 把代码提交到stage区 > git add ...

  9. ruby lib文件夹作用

    require 'lib/test_module' #lib/test_module.rb module TestModule end

  10. MySQL 温故知心(三)

    MySQL锁概述 相对其他数据库而言,MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制.比如,MyISAM和MEMORY存储引擎采用的是表级锁(table-level loc ...