LeetCode——Arithmetic Slices
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的更多相关文章
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Arithmetic Slices 算数切片
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- Leetcode: Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- Leetcode: Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- LeetCode 446. Arithmetic Slices II - Subsequence
原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is ...
- Leetcode之动态规划(DP)专题-413. 等差数列划分(Arithmetic Slices)
Leetcode之动态规划(DP)专题-413. 等差数列划分(Arithmetic Slices) 如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列. 例如,以下数列为 ...
- LeetCode 413 Arithmetic Slices详解
这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...
- 【LeetCode】413. Arithmetic Slices 等差数列划分
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 双指针 递归 动态规划 日期 题目地址:htt ...
- [LeetCode]413 Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
随机推荐
- 1.1 VGA(图像显示卡),Graphics Card(图形加速卡),Video Card(视频加速卡),3D Accelerator Card 和 GPU(图形处理器)
1.1 VGA(图像显示卡),Graphics Card(图形加速卡),Video Card(视频加速卡),3D Accelerator Card 和 GPU(图形处理器) 对这些概念之前也没怎么了解 ...
- js Ajax 跨域请求
一.使用jsonp的方式(只支持get请求) 二.使用cors的方式(支持HTTP的大部分请求方式) 三.apache的转发(修改服务器配置) 没有试验,暂时不详细写!
- https://zh.cppreference.com 和 https://en.cppreference.com 和 https://cppcon.org/
https://zh.cppreference.comhttps://en.cppreference.com/w/ https://cppcon.org/
- post 传递参数中包含 html 代码解决办法,js加密,.net解密
今天遇到一个问题,就是用post方式传递参数,程序在vs中完美调试,但是在iis中,就无法运行了,显示传递的参数获取不到,报错了,查看浏览器请求情况,错误500,服务器内部错误,当时第一想法是接收方式 ...
- 从HD OJ 1005想到的
杭电OJ [1005](http://acm.hdu.edu.cn/showproblem.php?pid=1005): #####Problem Description > A number ...
- python中的抽象方法
python中的抽象方法 父类要限制1.子类必须有父类的方法2.子类实现的方法必须跟父类的方法的名字一样 import abc class A(metaclass=abc.ABCMeta): @abc ...
- 思辨“从外至内的认识和表达”——By Me at 20140928
从下面几个维度,来思辨“从外至内的认识和表达” [思考维度1]提到研发前期的架构工作 ...
- 1.新建项目出现包名有一道红线The SDK platform-tools version ((23)) is too old to check APIs compiled with API 20
原因分析: 就是platform-tools的版本太低导致的 解决方法: 1.点开SDK Manager,打开SDK Tools面板,将Platform-tools更新 2.更新完之后重启as即可
- APIENTRY
1.在widnows编程中int APIENTRY WinMain中APIENTRY是什么意思,其什么作用? winapi表示此函数是普通的winapi函数调用方式,apientry则表明此函数是应用 ...
- 解决Centos关闭You have new mail in /var/spool/mail/root提示(转)
今天查看内存的时候 出现一天奇怪的提示 You have new mail in /var/spool/mail/root 有的时候每敲一下回车,就出来You have new mail in /va ...