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 ...
随机推荐
- git学习(7)标签管理
git学习(7)标签管理 1. 建立标签 在发布版本时候,我们通常会在版本库中打一个标签,这样就唯一确定了打标签的版本,有点像个里程碑,这里会有一个指向某个commit的指针 打标签很简单,首先切换到 ...
- 随笔 javascript-抽象工厂模式
随笔 javascript-抽象工厂模式 抽象工厂模式笔记 1.抽象工厂模式创建多个抽象类,创建出的结果是一个类簇(这里是抽象类的集合) 2.抽象工厂中传入的父类是否是抽象工厂方法创建的抽 ...
- <2014 10 01> 数学基础 Wikipedia
数学基础 数学上,数学基础一词有时候用于数学的特定领域,例如数理逻辑,公理化集合论,证明论,模型论,和递归论.但是寻求数学的基础也是数学哲学的中心问题:在什么终极基础上命题可以称为真? 目前占统治地位 ...
- 前端开发 - CSS - 总结
CSS:层叠样式表(Cascading Style Sheets) 1.css的特征2.css的引入3.选择器4.伪类选择器5.伪元素选择器6.字体样式 文本样式 背景属性7.盒模型 border m ...
- SKBUFFER详解
纯属转载,不敢侵犯别人产权!! 一. SKB_BUFF的基本概念1. 一个完整的skb buff组成(1) struct sk_buff--用于维护socket buffer状态和描述信息(2) he ...
- MySQL之备份恢复
1.备份的种类 逻辑备份:SQL语句的备份 物理备份:数据页备份 2.逻辑备份工具介绍 select xxxx from t1 into outfile '/tmp/redis.txt' mysql ...
- rabbitMQ基本概念
一.网页登录方法 http://127.0.0.1:15672/ 用户名和密码默认为guest/guest 用java代码去连接rabbitmq用的端口是5672 二.rabbitMQ基本概念 Rab ...
- Jenkins的安装及邮件配置
Jenkins介绍 Jenkins,是基于Java开发的一种持续集成工具,用于监控秩序重复的工作,包括: 1).持续的软件版本发布/测试项目. 2).监控外部调用执行的工作. Jenkins安装 j ...
- 深入理解Flink核心技术(转载)
作者:李呈祥 Flink项目是大数据处理领域最近冉冉升起的一颗新星,其不同于其他大数据项目的诸多特性吸引了越来越多的人关注Flink项目.本文将深入分析Flink一些关键的技术与特性,希望能够帮助读者 ...
- js中将Object转换为String函数代码
经常会碰到结果对象是object而无法查看该对象里面的内容而苦恼,有下面这个函数就好了,可以将其转化为字符串类型,然后就可以打印出来了,具体代码如下: function obj2string(o){ ...