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.
解题: 如果一个等差数列是长度是n,那么一共有一个 (n-1)(n-2)/2 个子等差数列,所以先求出所有最长的子等差数列的长度即可,利用滑动窗口,两个游标,当当前遇等差数列的条件不满足了以后
更新计数值和游标,代码如下:
public int numberOfArithmeticSlices(int[] A) {
if (A == null || A.length < 3) {
return 0;
} else {
int count = 0;
int left = 0, right = 1;
int diff = A[right] - A[left];
while (right < A.length) {
if (right < A.length - 1 && A[right + 1] - A[right] == diff) {
right++;
} else {
int length = right - left + 1;
if (length >= 3) {
count += ((1 + (length - 2)) * (length - 2)) / 2;
}
if (right == A.length - 1) {
break;
} else {
left = right;
right++;
diff = A[right] - A[left];
}
} }
return count;
}
}
[LeetCode]413 Arithmetic Slices的更多相关文章
-
LeetCode 413 Arithmetic Slices详解
这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...
-
LN : leetcode 413 Arithmetic Slices
lc 413 Arithmetic Slices 413 Arithmetic Slices A sequence of number is called arithmetic if it consi ...
-
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 Slice 算术序列切片(动态规划,暴力)
Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力) 题目描述 如果一个数组1.至少三个元素2.两两之间差值相同,那么这个数组就是算术序列 比如下面的数组都是算 ...
-
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 等差数列划分
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 双指针 递归 动态规划 日期 题目地址:htt ...
-
【Leetcode】413. Arithmetic Slices
Description A sequence of number is called arithmetic if it consists of at least three elements and ...
-
413. Arithmetic Slices
/**************************Sorry. We do not have enough accepted submissions.*********************** ...
-
LeetCode 446. Arithmetic Slices II - Subsequence
原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is ...
随机推荐
-
CG_INLINE,inline 内联函数
内联函数,即在编译的时候将函数体替换函数调用,从而不需要将parameter,return address进行push/pop stack的操作,从而加速app的运行,然而,会增加二进制文件的大小. ...
-
【转载】APP留存率多少才合格——全面解析留存率
做产品经理的一般都会关注以下 提高用户留存率 提高用户粘性和活跃度 这些天,有几位朋友都找我聊产品的留存率,有做手游的,做工具的,做社交APP的,于是把以前写过的留存率文章翻出来. 次日留 ...
-
sql-将字符串按指定字符分割插入指定表中
CREATE PROC SPLIT( @STR VARCHAR(MAX), --截取字符串 ), --截取字符 ) --存放表名 ) AS BEGIN ) IF(OBJECT_ID(@TABLE) I ...
-
ArcGIS中的坐标系定义与转换 (转载)
原文:ArcGIS中的坐标系定义与转换 (转载) 1.基准面概念: GIS中的坐标系定义由基准面和地图投影两组参数确定,而基准面的定义则由特定椭球体及其对应的转换参数确定,因此欲正确定义GIS系统坐 ...
-
[SQL]开启事物,当两条插入语句有出现错误的时候,没有错误的就插入到表中,错误的语句不影响到正确的插入语句
begin transaction mustt insert into student values(,'kkk','j大洒扫','j','djhdjh') insert into student v ...
-
查看Safari和钥匙串中的密码
Safari Safari的同步书签功能很棒,还可以看到其他设备没关掉的网页.为了省时间,一些经常进的网站,比如博客,邮箱等,我都会选择让Safari保存密码,还使用iCloud同步!因为一直很放心苹 ...
-
谁能告诉我war包的作用及使用方法。。。。。。
链接地址:http://zhidao.baidu.com/link?url=iliyTcmsTKb1K4gHMtWUsRIBaXglyOKIQsWwdrgvydvnaUHLe0KEoHvLVz8tLY ...
-
Cracking the Coding Interview 第一章
第一章:数组与字符串 1 数组与字符串 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,T ...
-
python之斐波那契数列递归推导在性能方面的反思
在各种语言中,谈到递归首当其冲的是斐波那契数列,太典型了,简直就是标杆 一开始本人在学习递归也是如此,因为太符合逻辑了 后台在工作和学习中,不断反思递归真的就好嘛? 首先递归需要从后往前推导,所有数据 ...
-
mycat下mysql jdbc connector使用高版本报PacketTooBigException异常
如下所示: 5.1.30切换为mysql-connector 5.1.46/40,报错,可参考https://blog.csdn.net/n447194252/article/details/7530 ...