413 Arithmetic Slices 等差数列划分
如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列。
例如,以下数列为等差数列:
1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9
以下数列不是等差数列。
1, 1, 2, 5, 7
数组 A 包含 N 个数,且索引从0开始。数组 A 的一个子数组划分为数组 (P, Q),P 与 Q 是整数且满足 0<=P<Q<N 。
如果满足以下条件,则称子数组(P, Q)为等差数组:
元素 A[P], A[p + 1], ..., A[Q - 1], A[Q] 是等差的。并且 P + 1 < Q 。
函数要返回数组 A 中所有为等差数组的子数组个数。
示例:
A = [1, 2, 3, 4]
返回: 3, A 中有三个子等差数组: [1, 2, 3], [2, 3, 4] 以及自身 [1, 2, 3, 4]。
详见:https://leetcode.com/problems/arithmetic-slices/description/
C++:
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int res=0,len=2,n=A.size();
for(int i=2;i<n;++i)
{
if(A[i]-A[i-1]==A[i-1]-A[i-2])
{
++len;
}
else
{
if(len>2)
{
res+=0.5*(len-1)*(len-2);
}
len=2;
}
}
if(len>2)
{
res+=0.5*(len-1)*(len-2);
}
return res;
}
};
参考:https://www.cnblogs.com/grandyang/p/5968340.html
413 Arithmetic Slices 等差数列划分的更多相关文章
- 【LeetCode】413. Arithmetic Slices 等差数列划分
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 双指针 递归 动态规划 日期 题目地址:htt ...
- 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 ...
- LC 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
Description A sequence of number is called arithmetic if it consists of at least three elements and ...
随机推荐
- Office WORD如何输入长下划线
选中一段文字,点击下划线按钮,可以添加下划线 同样,选中一段空格,点下划线,也可以添加下划线
- 2 TypeScript--Hello World
安装好TypeScript后,我们来完成第一个页面--Hello World 新建index.html文件: <!DOCTYPE html> <html> <head&g ...
- Ctags基本配置
一般linux系统都会自带ctags,也可输入"ctags"看有木有该命令.有的话速度配置吧,没有话yum install ctags安装吧. 打开vim 配置文件,要是没该文件就 ...
- druid 参考配置
mysql <!-- 配置druid连接池 --> <context:property-placeholder location="classpath:jdbc.prope ...
- MVC优缺点
1.通过把项目分成model view和controller,使得复杂项目更加容易维护. 2.没有使用view state和服务器表单控件,可以更方便的控制应用程序的行为 3.应用程序通过contro ...
- 20170111 ABAP技术小结(全半角转换)
DATA: it_po LIKE it_alv OCCURS 0 WITH HEADER LINE.************************************************** ...
- linux 下使用exp/imp 或者expdp/impdp导出导入oracle数据表数据
一.环境配置 1.执行环境: exp/imp可以在客户端执行也可以在服务器端执行,在客户端执行需要先安装有oracle的客户端,如果是linux系统,就是以oracle用户登录,在控制台下执行.建议在 ...
- 四:多线程--NSOperation简单介绍
一.NSOperation简介 1.NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQu ...
- Java并发思考
如果说传统金融政企软件主要看事务,而互联网软件主要是并发,就像淘宝双十一主要就是在解决并发问题. 并发的目的是提高系统效率,响应时间和吞吐量. Java中并发的基本单位是线程,如何更好的解决线程协作通 ...
- math.floor()函数的用法
floor() 返回数字的下舍整数. 语法 以下是 floor() 方法的语法: import math math.floor( x ) 注意:floor()是不能直接访问的,需要导入 math 模块 ...