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++);elsecount=1;}return sum;}
};

  

转载于:https://www.cnblogs.com/rgvb178/p/5967894.html

LeetCode - 413. Arithmetic Slices - 含中文题意解释 - O(n) - ( C++ ) - 解题报告相关推荐

  1. LeetCode 413. Arithmetic Slices

    题目: A sequence of number is called arithmetic if it consists of at least three elements and if the d ...

  2. leetcode 413. Arithmetic Slices | 413. 等差数列划分(Java)

    题目 https://leetcode.com/problems/arithmetic-slices/ 题解 一次遍历搞定,直接看草稿. class Solution {public int numb ...

  3. leetcode 446. Arithmetic Slices II - Subsequence | 446. 等差数列划分 II - 子序列(动态规划)

    题目 https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题解 找等差子数列,详见注释. 一个月前做过,没通过,今天 dai ...

  4. [leetcode] 309. Best Time to Buy and Sell Stock with Cooldown 解题报告

    题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ Say you have an ar ...

  5. Leetcode 473. Matchsticks to Square 卖火柴的小女孩画框框 解题报告

    1 解题思想 这道题的意思就是卖火柴的小女孩有一堆长度不等的火柴,他希望拼成一个正方形,不知道可以不可以? 所谓可以就是: 1.用了所有火柴,一根不多,一根不少,当然一根用一次 2.四条边长度一样 所 ...

  6. Arithmetic Slices

    这两天一直复习动态规划,就想到leetcode上刷刷题,easy难度的很少,大部分都是medium和hard.本题是第一道DP类型medium难度的题目,但是用其他的方法比如暴力法也可以求解.首先来看 ...

  7. 【完美解决:绝对路径含中文问题】bat一键启动应用绝对路径中含中文导致无法执行

    目录 问题由来 本篇文章将介绍: ①.bat文件如何写 ②代码每一步骤的解释 ③因电脑绝对路径中含有中文导致.bat文件无法启动的问题. .bat文件书写 .bat代码解释 .bat启动路径含中文报错 ...

  8. python读取文件名或路径含中文字符的图片并从中筛选出全白或者全黑的图片

    之前做样本的时候已经写过这部分代码 只不过筛选的是全黑的样本图和其对应的真实影像 那么当真实影像图包含白色图片而且文件名包含中文字符了怎么办 大家知道imread无法读取路径和图片名称含中文字符的图片 ...

  9. 剖析ifstream打开含中文路径名文件失败的原因

    http://blog.csdn.net/yukin_xue/article/details/7543423 最近写程序的时候遇到了使用ifstream打开含中文路径文件时失败的问题,在网上翻了一下, ...

最新文章

  1. 一位群友作为后端开发在滴滴和头条分别干了 2 年的经验总结
  2. python function if yield_Python中的yield关键字
  3. LeetCode OJ -- Binary Tree Paths
  4. 华章数学译丛目录(2020年7月补缺更新版,共73本)
  5. 关于AttributeError:‘Flask‘ object has no attribute ‘ensure_sync‘的报错解决
  6. android动画之布局动画,Android动画--布局动画 LayoutAnimation
  7. 2014 UESTC Training for Data Structures D - 长使英雄泪满襟
  8. GDB调试带参数argc argv的程序
  9. 高斯消元解线性方程组(浮点高斯消元模板)
  10. Oracle命令--查询语句
  11. 素数猜想对 c语言,1007 素数对猜想 (20 分)(C语言实现)
  12. 酒吧手机游戏java_酒吧里24种常见游戏的玩法
  13. java爬虫视频教程_JAVA开发教程:java视频教程java爬虫实战项目 百度网盘
  14. 常用服务和开放端口对照表
  15. 关于DBC文件的创建(DBC文件系列其一)
  16. ghostscript的坑
  17. msyql创建数据库并指定字符集
  18. Python要点及其环境搭建+Pycharm简单使用教程
  19. 你是人见人捏的“软柿子”吗
  20. NSDTF-DEM格式高程数据转通用的tiff格式高程数据

热门文章

  1. 转:如何进行软件架构设计?
  2. 和我一起作Tess的windbg lab - Lab3, Memory
  3. 图文方式管理Linux服务器(Webmin)
  4. 【正一专栏】春日随感—赢在起跑线上
  5. java shiro原理_Springboot shiro认证授权实现原理及实例
  6. python 求均值_数据分析:寻找Python最优计算性能
  7. Service Cloud: Quick Look
  8. Backbonejs之view的基本用法
  9. 可以将一个普通的Buffer转成只读的Buffer
  10. JAR软件转APK,在线转换器