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. 1, 3, 5, 7, 9
  2. 7, 7, 7, 7
  3. 3, -1, -5, -9

The following sequence is not arithmetic.

  1. 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:

  1. A = [1, 2, 3, 4]
  2. 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.代码

  1. public:
  2. int numberOfArithmeticSlices(vector<int>& A) {
  3. int sum=0,count=1;
  4. if(A.size()<3) return 0;
  5. for(int i=1;i<A.size()-1;i++)
  6. {
  7. if(A[i]-A[i-1]==A[i+1]-A[i])
  8. sum+=(count++);
  9. else
  10. count=1;
  11. }
  12. return sum;
  13. }
  14. };

  

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. 一个共通的viewModel搞定所有的编辑页面-经典ERP录入页面(easyui + knockoutjs + mvc4.0)

    http://www.cnblogs.com/xqin/archive/2013/06/06/3120887.html 前言 我写代码喜欢提取一些共通的东西出来,之前的一篇博客中说了如何用一个共通的v ...

  2. VirtualBox:Fatal:Could not read from Boot Medium! System Halted解决措施

    打开VirtualBox加载XP虚拟机操作系统时,出现含有下面文字的错误:   Could not read from Boot Medium! System Halted   或下面图中所示错误: ...

  3. JavaScript模块化学习基础

    http://www.ruanyifeng.com/blog/2012/10/javascript_module.html 一.原始写法 模块就是实现特定功能的一组方法. 不同函数简单放在一起就算一个 ...

  4. Java初学(八)

    一.图解String类创建对象及赋值 面试题: 二.String.StringBuffer.StringBuilder区别 1.String内容不可变,而StringBuffer.StringBuil ...

  5. IF IE

    1. <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]-->2. <!--[if IE]> 所有的IE可识别 & ...

  6. 如何做一名好的web安全工程师?

    在网络安全行业里面,web安全方向的人相对来说算是占大头,因为web安全初学阶段不像系统底层安全那么枯燥,而且成功hack目标网站的成就感相对也是比较强的. web安全工程师这个职位在甲方和乙方公司都 ...

  7. html表单验证程序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 用java做的免费投票器/软件/工具 可定制

    免费投票器/软件/工具 可定制 下载地址: http://pan.baidu.com/s/1c0je5HY 界面预览:

  9. SqlDataReader读取分页数据,pageCount你是肿么了?

    自己在折腾代码的时候发现,SqlDataReader读取分页数据,存储过程中的输出参数总页数pageCount获取不准确. 我已经问过百度,技术群等..... 都说SqlDataReader用过后关闭 ...

  10. Android相机、相册获取图片显示并保存到SD卡

    Android相机.相册获取图片显示并保存到SD卡 [复制链接]   电梯直达 楼主    发表于 2013-3-13 19:51:43 | 只看该作者 |只看大图  本帖最后由 happy小妖同学 ...