1. https://leetcode.com/problems/arithmetic-slices/
  2.  
  3. public class Solution {
  4. public int numberOfArithmeticSlices(int[] A) {
  5. if (A.length < 3) {
  6. return 0;
  7. }
  8.  
  9. int lastLen = 1;
  10. int lastDiff = A[1] - A[0];
  11.  
  12. int ret = 0;
  13. for (int i=2; i<A.length; i++) {
  14. if (A[i] - A[i-1] == lastDiff) {
  15. // 这一步,很重要,因为增加的个数正好是lastLen
  16. ret += lastLen;
  17. lastLen++;
  18. }
  19. else {
  20. lastLen = 1;
  21. lastDiff = A[i] - A[i-1];
  22. }
  23. }
  24. return ret;
  25. }
  26. }

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. DRF的@action装饰器

    # 转自:http://www.cnblogs.com/zhzhlong/p/9325180.html 视图集中附加action的声明 from rest_framework.decorators i ...

  2. jstorm系列-2:入门

    有了基本的概念之后,我们用jstorm来做一点小事情吧 做一个很无聊的事情:给定一个时间戳,输出对应的问候语 规则是:时间戳的十位对应的数字对应不同的时间段,0-2代表早上,3代表中午,4-6代表下午 ...

  3. linux命令(27):cat命令

    实例一:把 log2012.log 的文件内容加上行号后输入 log2013.log 这个文件里 cat -n log2012.log log2013.log 实例二:把 log2012.log 和 ...

  4. linux命令(5):netstat命令

    网络监控:netstat –in [显示所有配置接口的状态] 查看端口状态:netstat -anlp | grep 8080 [显示8080端口列出的监听状态] 查看某个进程软件名:netstat ...

  5. redis之(三)redis的数据类型

    [一]字符串类型(基本数据类型) --->字符串类型是redis的最基本的数据类型 --->能存储任何形式的字符串,(用户邮箱,json化的对象,一张图片) --->一个字符串类型的 ...

  6. 云联云通讯报错:应用与模板id不匹配,解决方法

    <statusMsg>应用与模板id不匹配</statusMsg> 这种一般是后端服务器上配置的APP ID和模板ID所属的APP不一致造成的 找到发送模板短信的方法,修改ap ...

  7. Linux的文件帮助和运行级别

    man命令相关:man -1 +参数 表示查询第几章的帮助说明man -k +参数 表示以该参数为关键字查询所有相关命令或文件命令 --help 简单查询命令使用说明具体的帮助文档存储在/usr/sh ...

  8. No module named '_Sqlite3' 解决方法

    今晚,在学习Python的时候,(学习链接:http://yidao620c.github.io/blog/20150420/simpleblog-01.html(搭载自己的博客案例)): 想为系统生 ...

  9. PDF文档盖章

    概述 在pdf文档的最后一页,合适位置,添加印章图片. maven依赖 <dependency> <groupId>com.itextpdf</groupId> & ...

  10. Did Pong Lie? (差分系统 判负环)

    Did Pong Lie? 时间限制: 5 Sec  内存限制: 128 MB提交: 68  解决: 15[提交][状态][讨论版] 题目描述 Doctor Pong has two arrays o ...