A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.

For example, these are arithmetic sequence:

1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9

The following sequence is not arithmetic.

1, 1, 2, 5, 7

A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 <= P < Q < N.

A slice (P, Q) of array A is called arithmetic if the sequence:
A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q.

The function should return the number of arithmetic slices in the array A.

Example:

A = [1, 2, 3, 4]

return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.


解题: 如果一个等差数列是长度是n,那么一共有一个 (n-1)(n-2)/2 个子等差数列,所以先求出所有最长的子等差数列的长度即可,利用滑动窗口,两个游标,当当前遇等差数列的条件不满足了以后
更新计数值和游标,代码如下:
public int numberOfArithmeticSlices(int[] A) {
if (A == null || A.length < 3) {
return 0;
} else {
int count = 0;
int left = 0, right = 1;
int diff = A[right] - A[left];
while (right < A.length) {
if (right < A.length - 1 && A[right + 1] - A[right] == diff) {
right++;
} else {
int length = right - left + 1;
if (length >= 3) {
count += ((1 + (length - 2)) * (length - 2)) / 2;
}
if (right == A.length - 1) {
break;
} else {
left = right;
right++;
diff = A[right] - A[left];
}
} }
return count;
}
}

[LeetCode]413 Arithmetic Slices的更多相关文章

  1. LeetCode 413 Arithmetic Slices详解

    这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...

  2. LN : leetcode 413 Arithmetic Slices

    lc 413 Arithmetic Slices 413 Arithmetic Slices A sequence of number is called arithmetic if it consi ...

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

  4. Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力)

    Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力) 题目描述 如果一个数组1.至少三个元素2.两两之间差值相同,那么这个数组就是算术序列 比如下面的数组都是算 ...

  5. Week 8 - 338.Counting Bits & 413. Arithmetic Slices

    338.Counting Bits - Medium Given a non negative integer number num. For every numbers i in the range ...

  6. 【LeetCode】413. Arithmetic Slices 等差数列划分

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 双指针 递归 动态规划 日期 题目地址:htt ...

  7. 【Leetcode】413. Arithmetic Slices

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

  8. 413. Arithmetic Slices

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

  9. LeetCode 446. Arithmetic Slices II - Subsequence

    原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is ...

随机推荐

  1. oracle 创建database Link

    当两台不同的数据库服务器想要共享一部分数据的时候,可以通过创建database Link的方式实现. 创建全局database links ,则必须使用systm或sys用户,在database前加p ...

  2. oracle 正则表达式

    1.获取不包含 欧洲|*北美|*中国|*欧拉非 字符串的行. SELECT/* count(*)*/*   FROM table t  WHERE NOT regexp_like(t.xxxx, '( ...

  3. [python] ORM 第一次注释

    不懂的东西还太多,就当是自己监督自己吧 #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'Michael Liao' impor ...

  4. The processing instruction target matching ''[xX][mM][lL]" is not allowed

    报错的来源是: <?xml version="1.0" encoding="UTF8"?> 解决方案::,一般是WSDL的头文件的格式出了问题,比如 ...

  5. 在xaf 14 中实现 Tonyyang原文中的action权限

    具体实现过程,主要修改了以下几个地方: 第一:角色和用户类中需要修改成SecurityStrategy的方式: 具体代码 MySecurityRole: using System; using Sys ...

  6. 不起眼的 z-index 却能牵扯出这么大的学问

    z-index在日常开发中算是一个比较常用的样式,一般理解就是设置标签在z轴先后顺序,z-index值大的显示在最前面,小的则会被遮挡,是的,z-index的实际作用就是这样. 但是你真的了解z-in ...

  7. mssql

    1.打开php.ini,将 ;extension=php_mssql.dll前面的分号(;)去掉,然后重启 Apache. 如果不行的话,进行第2步: 2.检查一下你的php安装目录下的ext下面有没 ...

  8. asp.net Word Document Open return null

  9. zTree树的模糊搜索

    工作需要,所以做了一个比较方便的搜索功能:1.功能实现都是基于zTree的API:2.如有更好的建议,欢迎拍我:其中要说明下的是flag 这个字段, 这是我自己定义的扩展字段,代码中涉及到flag 请 ...

  10. 實際案例: 已知要獲取臨時票証 (JsApi Ticket) 才能調用的接口

    需獲取票証才能調用的接口,簡單列示如下: 一.基礎類 1. wx.checkJsApi (當前客戶端是否支持指定JS) 二.分享類 1.wx.onMenuShareTimeline (分享到朋友圈)2 ...