LC 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.
Runtime: 4 ms, faster than 35.11% of C++ online submissions for Arithmetic Slices.
这一题的slice是连续的,所以只要判断i, i-1, i-2的关系然后递推就行了。
dp[i] = dp[i-1] + 1, 这里的dp[i]是以index i结尾的等差数列。 意思是,如果当前的位置能和之前两个位置组成等差数列,那么该位置上之前的的等差数列就等于
之前一个位置上结尾的(dp[i-1])数量(因为如果之前的位置上有相同差值的等差数列,再连接上以index i结尾的等差数列,还是可行的)然后再加上1,这个1
是以当前位置作为结尾,用前面的两个数合并起来的一个新的三个数的等差数列,这在之前的情况中是没有出现的。
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int ret = , cur = , n = A.size();
for(int i=; i<n; i++){
if(A[i] - A[i-] == A[i-] - A[i-]){
cur += ;
ret += cur;
}else {
cur = ;
}
}
return ret;
}
};
LC 413. Arithmetic Slices的更多相关文章
- LN : leetcode 413 Arithmetic Slices
lc 413 Arithmetic Slices 413 Arithmetic Slices A sequence of number is called arithmetic if it consi ...
- 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 ...
- LeetCode 413 Arithmetic Slices详解
这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...
- [LeetCode]413 Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- 413. Arithmetic Slices
/**************************Sorry. We do not have enough accepted submissions.*********************** ...
- 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 ...
- 【Leetcode】413. Arithmetic Slices
Description A sequence of number is called arithmetic if it consists of at least three elements and ...
- LeetCoce 413. Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- 【LeetCode】413. Arithmetic Slices 等差数列划分
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 双指针 递归 动态规划 日期 题目地址:htt ...
随机推荐
- web端文件上传,预览,下载,删除
//HTML部分 <div class="item attachment attachmentNew"> <span class="name&quo ...
- 彻底卸载mysql数据库~
以下操作以win10操作系统为例 1 停止window的MySQL服务 打开此台电脑的管理 ---> 服务和应用程序 --->服务,找到mysql的服务并停止 2 卸载MySQL安装程序 ...
- 命令行工具--curl
目录 命令:curl 一.简介 二.使用案例 1.基本用法 2.保存访问的网页 3.测试网页返回值 4.指定proxy服务器以及其端口 5.cookie 6.模仿浏览器 7.伪造referer(盗链) ...
- P2664 树上颜色统计 点分治 虚树 树上差分 树上莫队
树上差分O(n)的做法 考虑每种颜色对每个点的贡献,如果对于每种颜色我们把当前颜色的点删除,那么原来的树就会分成几个子树,对于一个点,当前颜色在和他同子树的点的点对路径上是不会出现的.考虑到有多种颜色 ...
- Linux之RPM 软件管理程序
RPM RPM是软件管理程序,提供软件的安装.升级.查询.反安装的功能.优点:a.安装方便,软件中所有数据都经过编译和打包b.查询.升级.反安装方便缺点:a.缺乏灵活性b.存在相依属性 用法: rpm ...
- mybatis中#{}和${}的区别(转载)
原文地址:http://www.cnblogs.com/baizhanshi/p/5778692.html 1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by ...
- shell进阶--流程
由于条件判断和循环跟其他语言都大同小异,学过编程的话很好理解,这里只贴出格式,不具体写用法了.(select菜单会详细讲一下) 条件判断 if条件判断 普通if条件判断: 嵌套if条件判断: 循环 f ...
- nginx之"/"结尾
一.配置127.0.0.1:8081的tomcat下的文件: 1.ROOT/a.html : this is ROOT page 2.ROOT/testa.html : nihao 3.ROOT/in ...
- PPM / PGM / PBM 图像文件格式[转]
下面将详细介绍ppm文件 ppm文件是一种图像文件,有其自己的文件格式.ppm文件由两个部分组成:第一个部分是三行ASCII码,这个部分决定了图像的存储格式以及图像的特征:第二个部分就是图像的数据部分 ...
- BZOJ 4278: [ONTAK2015]Tasowanie (后缀数组 / 二分+hash)
直接归并,然后如果哪边的后缀字典序比较小就去哪边,然后就可以后缀数组 博客传送门- 但是本蒟蒻不会后缀数组 Upd:Upd:Upd:现在会了233.一道差不多的题:BZOJ 1692: [Usaco2 ...