1.题目大意

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.

刚开始看这道题的时候我还挺纠结的,然后发现其实是我对题意的理解有问题,或者说题目本身sample给的也不好。

(1)首先,题目输入的数列是0索引的数组,注意,这个输入的数组的各个数字不一定是等差数列,比如说可能输入的是{1,2,5,6}。

(2)所求的P+1<Q,也就是说所求出的等比数列中至少包含3个元素,比如{1,2,3,4}中{1,2}就不算是arithmetic。

(3)所求的arithmetic在原数组中要求是位置连续的,比如原数组若给定的是{1,2,4,3},这里的{1,2,3}在原数组中并不连续,因此会返回0。而{1,3,5,6}中{1,3,5}位置则是连续的。

再举几个例子:

输入:{1,2} 返回0 。因为没有3个以上的元素。

输入:{1,3,5,6,7} 返回2。因为有{1,3,5}和{5,6,7}。而{1,3,5,7}则不算,因为它们在原数组里不连续。

输入:{1,3,5} 返回1。

2.思路

显而易见:连续的等差数列有n个元素的时候,则会有(1+2+...+n)个等差子数列。

{1,2,3} -> {1,2,3} ->1

{1,2,3,4} -> {1,2,3,4} + {1,2,3} + {2,3,4} -> 1+2 ->3

{1,2,3,4,5} -> {1,2,3,4,5} + {1,2,3,4} + {2,3,4,5} + {1,2,3} + {2,3,4} + {3,4,5} -> 1+2+3 ->6

{1,2,....,n} -> ...... -> 1+2+...+n

3.代码

public:
int numberOfArithmeticSlices(vector<int>& A) {
int sum=0,count=1;
if(A.size()<3) return 0;
for(int i=1;i<A.size()-1;i++)
{
if(A[i]-A[i-1]==A[i+1]-A[i])
sum+=(count++);
else
count=1;
}
return sum;
}
};

  

LeetCode - 413. Arithmetic Slices - 含中文题意解释 - O(n) - ( C++ ) - 解题报告的更多相关文章

  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

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

  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】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  7. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

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

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

  9. 【Leetcode】413. Arithmetic Slices

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

随机推荐

  1. 增强型for循环,用于遍历数组元素

    /** * */ package com.cn.u4; /** * @author Administrator *增强型for */ public class ZhengQiangFor { publ ...

  2. UVA1262Password(第K字典序)

    题目链接 紫书P323 题意:两个6*5的字母矩阵,两个矩阵每列相同的字母,每列取一个,求按照字典序第k小的序列 分析: 对于第一个样例来说,我们得到{ACDW}.{BOP}.{GMOX}.{AP}. ...

  3. easyui之datagrid的使用

    http://www.cnblogs.com/ruanmou001/p/3840954.html 一.神马是easyui jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery ...

  4. 关于updateElement接口

    1.bool UpdateGameElement(const struct_game_element& ele, gs_dbs_user_info_op_req& db_req, :: ...

  5. Linux目录结构及常用命令(转载)

    一.Linux目录结构 你想知道为什么某些程序位于/bin下,或者/sbin,或者/usr/bin,或/usr/sbin目录下吗?例如,less命令位于/usr/bin目录下.为什么没在/bin中,或 ...

  6. Enterprise Library系列文章目录(转载)

    1. Microsoft Enterprise Library 5.0 系列(一) Caching Application Block (初级) 2. Microsoft Enterprise Lib ...

  7. 新浪微博客户端(9)-实现版本新特性的ViewPager

    "DJNewFeatureViewController.m" #import "DJNewFeatureViewController.h" #define NE ...

  8. jQuery特效

    基础特效 方法 描述 hide() 立即隐藏jQuery对象内的所有元素 hide(time).hide(time, easing) 在指定的时间内以动画方式隐藏jQuery对象内的所有元素,并可选一 ...

  9. C开发基础--函数调用栈

    发现有一些问题几乎是所有的新人都会遇到,而且也常因为缺乏一些基本的知识而无从下手.函数调用栈的内容就是其中之一.于是花点时间把以前写的内容整理出来. 程序在运行期间,内存中有一块区域,用来实现程序的函 ...

  10. php设置cookie,在js中如何获取

    在php中设置cookie. $email="abc@163.com"; setcookie("y_email",$email,time()+3600,&quo ...