/**************************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;
 }

最新文章

  1. WWDC 2013 Session笔记 - iOS7中的多任务
  2. c++实现蛇形矩阵总结
  3. string.Format之你不知道的事
  4. combotree
  5. Careercup - Microsoft面试题 - 5204967652589568
  6. python-操作mssql数据库
  7. add-two-numbers-ii
  8. 第2章 Git安装与设置
  9. zigbee
  10. BZOJ 1030 [JSOI2007]文本生成器
  11. Android -- 利用Broadcast开启Service(转)
  12. sqlserver的rownum
  13. NVIDIA Titan Xp Star Wars Collector&#39;s Edition显卡深度学习工作站 + Ubuntu17.10 + Tensorflow-gpu + Anaconda3 + Python 3.6 设置
  14. java爬虫系列第五讲-如何使用代理防止爬虫被屏蔽?
  15. Java 实现String语句的执行(Jexl)
  16. Codeforces 1025 D - Recovering BST
  17. nginx ssi + ngx_pagespeed 实现micro frontends 开发
  18. React-Router v4.0 hashRouter使用js跳转
  19. BZOJ3551: [ONTAK2010]Peaks加强版【Kruskal重构树】【主席树】
  20. akka开发(一)HelloWorld

热门文章

  1. java之Timer
  2. Java之properties文件读取
  3. mongodb university week4
  4. MAC地址,使用java获取IP地址和MAC地址。
  5. 安装sqlserver2012时出现的丧心病狂的错误
  6. nhibernat4.0.0.4000 bug
  7. Sqli-LABS通关笔录-14
  8. 计蒜客 删除字母&#39;c&#39;
  9. Codeforces 519 E. A and B and Lecture Rooms
  10. windows下C语言编程获取磁盘(分区)使用情况