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 ...
随机推荐
- 猫猫学iOS 之第一次打开Xcode_git配置,git简单学习
猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:错误 当第一次打开Xcode我们进行commit操作的时候会 ...
- MFC项目实战(1)文件管理器--界面设计篇
1.创建项目 文件管理器是一个基于对话框的应用程序.首先新建一个“MFC应用程序”类型的项目,然后输入新建项目的名称“FileMng”,并指定该项目保存的位置,单击“确定”按钮,如图所示. 在弹出的“ ...
- Lucene Core Solr
Apache Lucene - Welcome to Apache Lucene https://lucene.apache.org/ The Apache LuceneTM project deve ...
- command 'gcc' failed with exit status 1
https://stackoverflow.com/questions/11094718/error-command-gcc-failed-with-exit-status-1-while-insta ...
- 利用chrome调试手机网页
1.pc端安装最新的chrome 2.手机端安装最新的chrome ( Android机 )ms不需要 3.USB连接线 4.打开电脑的chrome 在地址栏输入 chrome://inspect
- jquery中注意点
1.jquery.fn.extend和jquery.extend jquery.fn.extend是向jquery中的prototype中添加方法或者属性,而jquery.extend是向jquery ...
- YTU 2558: 游起来吧!超妹!
2558: 游起来吧!超妹! 时间限制: 1 Sec 内存限制: 128 MB 提交: 70 解决: 22 题目描述 夏天到了,无聊的超妹跑到了落雪湖里抓鱼吃.结果,游到湖的正中 央时被湖边保安看 ...
- Silverlight数据绑定之DataGrid
Silverlight数据绑定之DataGrid 时间:2011-08-03 01:59来源:网易博客 作者:Wilson. 点击:次 注:所有代码以C#为例 DataGrid绑定的数据对象: 1.D ...
- sphinx 针对tedfield搜索
query = "(user can be admin)" -> check all fields for the given words. If all words ar ...
- 并不对劲的bzoj4198:loj2132:uoj130:p2168:[NOI2015]荷马史诗
题目大意 有\(n\)(\(n\leq10^5\))种单词,其中第\(i\)种单词在文章中的出现次数为\(w_i\) 要将每个单词替换成一个字符集为\(k\)(\(k\leq9\))的字符串,使对于任 ...