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 ...
随机推荐
- HDU-5335
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- IntelliJ IDEA SpringBoot 使用第三方Tomcat以及部署
花了半天时间终于成功,记录以备查阅. 一.第三方Tomcat部署 部署部分参考的是:把spring-boot项目部署到tomcat容器中 目标:把spring-boot项目按照平常的web项目一样发布 ...
- rabbitmq源码安装及配置文件管理
rabbitmq 源码安装 官网地址:rabbitmq http://www.rabbitmq.com/releases/rabbitmq-server/ 官网地址:erlang http://erl ...
- mysql-表完整性约束
阅读目录 一 介绍 二 not null与default 三 unique 四 primary key 五 auto_increment 六 foreign key 七 总结 一 介绍 回到顶 ...
- String的hashCode分析
/** * Returns a hash code for this string. The hash code for a * {@code String} object is computed a ...
- 五十四 网络编程 TCP编程
Socket是网络编程的一个抽象概念.通常我们用一个Socket表示“打开了一个网络链接”,而打开一个Socket需要知道目标计算机的IP地址和端口号,再指定协议类型即可. 客户端 大多数连接都是可靠 ...
- B. Black Square(字符串)
B. Black Square time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Bzoj 3498 Cakes(三元环)
题面(权限题就不放题面了) 题解 三元环模板题,按题意模拟即可. #include <cstdio> #include <cstring> #include <vecto ...
- 洛谷——P1626 象棋比赛
P1626 象棋比赛 题目描述 有N个人要参加国际象棋比赛,该比赛要进行K场对弈.每个人最多参加两场对弈,最少参加零场对弈.每个人都有一个与其他人不相同的等级(用一个正整数来表示). 在对弈中,等级高 ...
- 【tomcat】tomcat远程调试
修改tomcat bin目录下的catalina.sh,增加下面这行: CATALINA_OPTS="-agentlib:jdwp=transport=dt_socket,address=9 ...