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 ...
随机推荐
- 分享一个检测用户是否用手机(Mobile)访问网站的 PHP 类
有一个基于MIT License协议开源的PHP程序 http://code.google.com/p/php-mobile-detect/ 程序就是一个文件,下载之后直接引用就可以. 使用方法: & ...
- HDU1306 String Matching 【暴力】
String Matching Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- oracle随机数
1.从表中随机取记录 select * from (select * from staff order by dbms_random.random) where rownum < 4 表示从ST ...
- java实现从报文中获取投保单号
java实现从报文中获取投保单号 投保单号正则表达式: String regex = "<proposalNo>([0-9]+)</proposalNo>[\\s\\ ...
- [libcurl]_[0基础]_[使用libcurl下载大文件]
场景: 1. 在Windows编程时, 下载http页面(html,xml)能够使用winhttp库,可是并非非常下载文件,由于会失败. 由此引出了WinINet库,无奈这个库的稳定性比較低,使用样例 ...
- 解决CentOS java环境不生效的问题
查看当前java版本 [root@localhost jdk1.6.0_45]# java -version openjdk version "1.8.0_65" OpenJDK ...
- GEO,IGSO,MEO,LEO
GEO(Geosynchronous Eearth Orbit):地球静止轨道卫星 IGSO(Inclined Geosynchronous Satellite Orbit):倾斜轨道同步卫星 地球同 ...
- IsNumeric 判断字符串是否为数字(使用Val函数实现),这个函数相当于Java的IsNaN函数
IsNumeric 判断字符串是否为数字,如果是数字返回true,如果包含有汉字或字符的话返回false. 由于Delphi本身没有IsNumeric这个函数,不像其它语言,这个函数相当于Java的I ...
- [NOIP2012] day2 T3疫情控制
题目描述 H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树,1 号城市是首都,也是树中的根节点. H 国的首都爆发了一种危害性极高的传染病.当局为了控制疫情,不让疫情扩散到 ...
- scikit-learn(1) 第一个例子说明
第一个 scikit-learn例子 ................................................................................. ...