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 ...
随机推荐
- java的IO流之字符流
# 原创,转载请留言联系 输出流 FileWriter类 常见的构造方法: FileWriter(String fileName) 根据给定的文件名构造一个 FileWriter 对象.Fil ...
- Hive SQL 常用日期
Hive SQL 常用日期 原文地址:Hive SQL常用日期函数 Hive SQL 常用日期 注意: MM DD MO TU等要大写 已知日期 要求日期 语句 结果 本周任意一天 本周一 selec ...
- hdu 1133(卡特兰数变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1133 题意:排队买50块一张的票,初始票台没有零钱可找,有m个人持有50元,n人持有100元, ...
- 【UI】自动化用例设计技巧
需要封装的方法: 公共的操作方法 经常使用的步骤:超过两次以上 经常使用的组件:输入框.文本框.列表 经常操作的布局:多个组件组成通用的布局 经常操作的页面:ui页面由一个一个单独Activity组成 ...
- VS Code使用
VS Code使用了有一段时间了,感觉各方面表现蛮好的,当然主要还是基于electron开发的,(有源代码,想改啥就改啥,当然现在也没有改什么,没那么时间,也没有那么多精力),性能不错,其实中间主要还 ...
- AC日记——可怜的狗狗 洛谷 P1533
可怜的狗狗 思路: 主席树第k大: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300001 #defin ...
- Web测试中容易被忽略的Charset问题
今天继续进行一个更综合的脚本制作,录制设置.进行录制.脚本修改,一切都轻车熟路,进行得很顺利.经过近一个小时的对比和修改,OK,脚本大功告成,终于可以小试牛刀了,嘿嘿. 运行,replay lo ...
- 延长SSH的连接时间并重启ssh服务
用SSH登录到Linux的时候,由于默认的连接超时时间很短,经常需要短了后再连接,比较麻烦.可以修改下sshd的配置文件,然后重启sshd服务即可: 1.#vim /etc/ssh/sshd_conf ...
- http测试工具ab
转载:https://www.cnblogs.com/ym123/archive/2015/07/08/4629735.html ab的全称是Apache Bench,是Apache自带的网络压力测试 ...
- ubuntu安装wine
1.安装源 sudo add-apt-repository ppa:wine/wine-builds sudo apt-get update 2.安装wine sudo apt-get install ...