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的更多相关文章

  1. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  2. [LeetCode] Arithmetic Slices 算数切片

    A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...

  3. [LeetCode]413 Arithmetic Slices

    A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...

  4. 413. Arithmetic Slices

    /**************************Sorry. We do not have enough accepted submissions.*********************** ...

  5. 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 ...

  6. Leetcode: Arithmetic Slices

    A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...

  7. [Swift]LeetCode413. 等差数列划分 | Arithmetic Slices

    A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...

  8. Arithmetic Slices II - Subsequence LT446

    446. Arithmetic Slices II - Subsequence Hard A sequence of numbers is called arithmetic if it consis ...

  9. Arithmetic Slices LT413

    A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...

  10. LeetCode——Arithmetic Slices

    Question A sequence of number is called arithmetic if it consists of at least three elements and if ...

随机推荐

  1. ExecutorService 用例

    import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Tes ...

  2. MySQL 的数据存储引擎

    MySQL的存储引擎 InnoDB: MySQL5.5之后的默认存储引擎. 采用MVCC来支持高并发,并且实现了四个标准的隔离级别(默认可重复读). 支持事务,支持外键.支持行锁.非锁定读(默认读取操 ...

  3. 邂逅Sass和Compass之Sass篇

    对于一个从后台转到前端的web开发者来说,最大的麻烦就是写CSS,了解CSS的人都知道,它可以开发网页样式,但是没法用它编程,感觉耦合性相当的高,如果想要方便以后维护,只能逐句修改甚至重写相当一部分的 ...

  4. 获取或设置config节点值

    ExeConfigurationFileMap 这个类提供了修改.获取指定 config 的功能:新建一个 ExeConfigurationFileMap 的实例 ecf :并设置 ExeConfig ...

  5. CodeForces 732F Tourist Reform

    边双连通分量. 这题有一点构造的味道.一个有向图,经过强连通缩点之后会形成一个有向无环图. 如果将最大的强连通分量放在顶端,其余的强连通分量都直接或间接指向他,那么这样就构造出了符合要求的图. 接下来 ...

  6. 洛谷P1850换教室

    题目传送门 理解题意:给定你一个学期的课程和教室数量以及教室之间的距离还有换教室成功的概率,求一个学期走的距离的期望最小值 题目是有够恶心的,属于那种一看就让人不想刷的题目...很明显的动规,但是那个 ...

  7. Spring源码分析之Bean的加载流程

    spring版本为4.3.6.RELEASE 不管是xml方式配置bean还是基于注解的形式,最终都会调用AbstractApplicationContext的refresh方法: @Override ...

  8. Binary Tree Longest Consecutive Sequence -- LeetCode

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  9. BZOJ 2631 tree(动态树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2631 [题目大意] 要求支持链修改,链查询,边断开,连接操作 [题解] 链修改分乘和加 ...

  10. 【线段树】hdu6183 Color it

    题意: 维护一个数据结构,支持三种操作: ①在平面上(x,y)处添加一个颜色为c的点. ②询问平面上(1,y1)-(x,y2)范围内,有多少种不同颜色的点. ③清除平面上所有点. 颜色数量很少,对于每 ...