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

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.

http://www.cnblogs.com/rgvb178/p/5967894.html

刚开始看这道题的时候我还挺纠结的,然后发现其实是我对题意的理解有问题,或者说题目本身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。

http://blog.csdn.net/camellhf/article/details/52824234

 //https://discuss.leetcode.com/topic/62992/3ms-c-standard-dp-solution-with-very-detailed-explanation
 class Solution {
     // 2nd round        date: 2016-10-15        location: Vista Del Lago III Apartement
 public:
     int numberOfArithmeticSlices(vector<int>& A) {
         )   ;
         vector<);
         ;
         ; i < A.size(); i ++) {
             ] == A[i - ] - A[i - ])
                 dp[i] = dp[i - ] + ;
             res += dp[i];
         }
         return res;
     }
 };
 //https://discuss.leetcode.com/topic/62162/3ms-question-maker-solution-in-cpp-o-n-time-and-in-space
 class Solution {
 public:
     int numberOfArithmeticSlices(vector<int>& A) {
         ) ;
         int size = (int) A.size();
         ; i < size - ; i++) {
             A[i] = A[i + ] - A[i];
         }
         A.resize(size - );
         size--;

         ;
         ;
         ; i < size; i++) {
             ]) {
                 res += len * (len - ) / ;
                 len = ;
             } else {
                 len++;
             }
         }
         ) res += len * (len - ) / ;
         return res;
     }
 };
 int numberOfArithmeticSlices(int* A, int ASize) {
     ;
     ;
     ) ;

     ; i < ASize-; ++i) {
         ] == A[i+]-A[i]){
             cnt += ending;
             ++ending;
         }else {
             ending = ;
         }
     }
     return cnt;
 }

413. Arithmetic Slices的更多相关文章

  1. LN : leetcode 413 Arithmetic Slices

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

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

  3. LeetCode 413 Arithmetic Slices详解

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

  4. [LeetCode]413 Arithmetic Slices

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

  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】413. Arithmetic Slices

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

  7. LeetCoce 413. Arithmetic Slices

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

  8. LC 413. Arithmetic Slices

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

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

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

随机推荐

  1. windows 下wamp环境3 安装php7

    打开 http://php.net 点击download,选择Windows downloads,根据系统选择版本 注意左侧的提示: With Apache you have to use the T ...

  2. C#GDI+基础(三)画刷详解

    SolidBrush:一般的画刷,通常只用一种颜色去填充GDI+图形 创建一般画刷: SolidBrush sbBrush1 = new SolidBrush(Color.Green); HatchB ...

  3. 关于outerWidth()属性

    在写代码的时候,获取元素的宽度通常用到这个属性.此属性具有如下特点: 1.默认情况下,它的值为所有后代元素(含此元素本身)中最大的宽度值. 2.若某后代元素的display属性为none,那么在计算的 ...

  4. iOS开发——源代码管理——SVN

    一.源代码管理(svn)简介 01. 源代码管理工具概述 ======================================================================= ...

  5. 问题: Oracle Database 10g 未在当前操作系统中经过认证

    问题: Oracle Database 10g 未在当前操作系统中经过认证 在Windows 7中安装Oracle 10g. 使用的Orcale版本是10g. 步骤1: 在Orcale官网上下载,下载 ...

  6. [转载]MySQL5.5 配置文件 my.ini 1067错误

    原文链接:http://blog.csdn.net/xiaochunyong/article/details/7438200 解压mysql-5.5.22-win32.zip后的目录中有5个my-xx ...

  7. 跟着百度学PHP[4]OOP面对对象编程-11-Final关键字

    Final的作用就是不允许儿子继承夫类,也就是说不能够对父类在进行调用,否则将会出错. 目录------------------------------------------------------ ...

  8. 快速排序(Quicksort)

    快速排序:是对冒泡排序的一种改进. 什么是冒泡排序:简单一点就是冒气泡.极值数据会到达数据的顶端. 实现步骤:建立一个数据排列标准,从大到小还是从小到大. [从大到小排列]:从第一个数据开始遍历,比较 ...

  9. qt-5.6.0 移植之实现板子与ubuntu主机通过网络进行文件传输

    经过一上午的调试以及同事的帮助,终于实现板子与主机的文件传输. 第一步关闭所有的防火墙 在 Windows 里面是在控制面板->安全->Windows 防火墙->自定义设置 在ubu ...

  10. linux/windows下启用和停止VMware后台服务的脚本

    linux/windows下启用和停止VMware后台服务的脚本 linux/windows下启用和停止VMware后台服务的脚本 linux平台 windows平台 本文由乌合之众 lym瞎编,欢迎 ...