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. linux下输入密码不回显

    这几天在做一个登陆的程序,需要将输入的密码屏蔽掉,自己百度,找到了两种方法,先贴下第一种方法, #include<stdio.h> #include<unistd.h> int ...

  2. 初次接触express

    今天初次使用express,还是写写心得好了. 中间件 mothod nodemon ~的使用 中间件 中间件我觉得就是个开箱即用的工具,写好中间件函数,直接use就好. 示例1: let myLog ...

  3. SGU 261. Discrete Roots

    给定\(p, k, A\),满足\(k, p\)是质数,求 \[x^k \equiv A \mod p\] 不会... upd:3:29 两边取指标,是求 \[k\text{ind}_x\equiv ...

  4. AC日记——[HNOI2010]BOUNCE 弹飞绵羊 洛谷 P3203

    [HNOI2010]BOUNCE 弹飞绵羊 思路: SBlct: 代码: #include <bits/stdc++.h> using namespace std; #define max ...

  5. Python基础系列----语法、数据类型、变量、编码

    1.基本语法                                                                                        Python ...

  6. webpack分离第三方库(CommonsChunkPlugin并不是分离第三方库的好办法DllPlugin科学利用浏览器缓存)

    webpack算是个磨人的小妖精了.之前一直站在glup阵营,使用browserify打包,发现webpack已经火到爆炸,深怕被社区遗落,赶紧拿起来把玩一下.本来只想玩一下的.尝试打包了以后,就想启 ...

  7. windows 10 的开始菜单里面图标右击弹不出选项菜单了

    组策略的问题 win + r => gpedit.msc 计算机配置->管理模板>“开始”菜单和任务栏>在“开始”菜单中禁用上下文菜单  改为“已禁用”

  8. Druid 架构

    本篇译自 Druid 项目白皮书部分内容( https://github.com/apache/incubator-druid/tree/master/publications/whitepaper/ ...

  9. mysql source 乱码

    mysql -u root -p --default-character-set=utf8 use dbname source /root/newsdata.sql

  10. [BZOJ3167][P4099][HEOI2013]SAO(树形DP)

    题目描述 Welcome to SAO ( Strange and Abnormal Online).这是一个 VR MMORPG, 含有 n 个关卡.但是,挑战不同关卡的顺序是一个很大的问题. 有 ...