arithmetic-slices
https://leetcode.com/problems/arithmetic-slices/
public class Solution {
public int numberOfArithmeticSlices(int[] A) {
if (A.length < 3) {
return 0;
}
int lastLen = 1;
int lastDiff = A[1] - A[0];
int ret = 0;
for (int i=2; i<A.length; i++) {
if (A[i] - A[i-1] == lastDiff) {
// 这一步,很重要,因为增加的个数正好是lastLen
ret += lastLen;
lastLen++;
}
else {
lastLen = 1;
lastDiff = A[i] - A[i-1];
}
}
return ret;
}
}
arithmetic-slices的更多相关文章
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] 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
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: Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- [Swift]LeetCode413. 等差数列划分 | Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- Arithmetic Slices II - Subsequence LT446
446. Arithmetic Slices II - Subsequence Hard A sequence of numbers is called arithmetic if it consis ...
- Arithmetic Slices LT413
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- LeetCode——Arithmetic Slices
Question A sequence of number is called arithmetic if it consists of at least three elements and if ...
随机推荐
- windows7配置python和django的开发环境
直接上图,这是我在我的电脑配置windows7python和django开发环境的所有用到的软件 要求不高,只需要这几个软件的版本相一致就行, 需要注意的是软件安装时需要统一是32位或者64位的软件, ...
- Flash中的注册点和中心点
用一句话概括注册点和中心点的作用,那就是:注册点用来定位,中心点用来变形 当然,这句话不是非常准确,只是暂时先这么理解,下面会详细讲解. 认识注册点 每个元件都有一个注册点.在元件编辑窗口.或在舞台中 ...
- hdu 2389(二分图hk算法模板)
Rain on your Parade Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 655350/165535 K (Java/Ot ...
- python读写hdf5及cdf格式文件
Python write and read hdf5 file http://stackoverflow.com/questions/20928136/input-and-output-numpy-a ...
- WordPress 性能优化:为什么我的博客比你的快
WordPress 很慢? 很多博主都会感觉 WordPress 很慢?作为全世界最常用的建站和博客系统 WordPress 来说,在性能设计上肯定不会有太大的问题,WordPress 开发团队也肯定 ...
- 微信小程序 - 仿南湖微科普小程序游戏环节
最近看到南湖微科普小程序游戏环节感觉还可以,于是模仿了下 <view class='current' animation="{{animation}}"> {{curr ...
- javascript实现与后端相同的枚举Enum对象
; (function (global, undefined) { global.Enum = function (namesToValues) { var enumeration = functio ...
- vault-in-kubernetes
http://www.devoperandi.com/vault-in-kubernetes-take-2/ https://www.usenix.org/sites/default/files/co ...
- 一个纠结的Linux定时任务
昨天写了一个Linux定时任务,搞了半天才是搞好,现在分享下我犯得错误 首先在Linux根目录下创建一个目录 mkdir cat_crazy 进去创建一个shell脚本test.sh,内容是: #!/ ...
- HRBUST 1213 单词接龙
暴力搜索. 按照能配对的关系建立有向边,然后暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #in ...