413. Arithmetic Slices

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

 

 

 1 //https://discuss.leetcode.com/topic/62992/3ms-c-standard-dp-solution-with-very-detailed-explanation
 2 class Solution {    
 3     // 2nd round        date: 2016-10-15        location: Vista Del Lago III Apartement
 4 public:
 5     int numberOfArithmeticSlices(vector<int>& A) {
 6         if (A.size() < 3)   return 0;
 7         vector<int> dp(A.size(), 0);
 8         int res = 0;
 9         for (int i = 2; i < A.size(); i ++) {
10             if (A[i] - A[i - 1] == A[i - 1] - A[i - 2])
11                 dp[i] = dp[i - 1] + 1;
12             res += dp[i];
13         }
14         return res;
15     }
16 };

 

 

 1 //https://discuss.leetcode.com/topic/62162/3ms-question-maker-solution-in-cpp-o-n-time-and-in-space
 2 class Solution {
 3 public:
 4     int numberOfArithmeticSlices(vector<int>& A) {
 5         if (A.size() < 3) return 0;
 6         int size = (int) A.size();
 7         for (int i = 0; i < size - 1; i++) {
 8             A[i] = A[i + 1] - A[i];
 9         }
10         A.resize(size - 1);
11         size--;
12         
13         int res = 0;
14         int len = 1;
15         for (int i = 1; i < size; i++) {
16             if (A[i] != A[i - 1]) {
17                 res += len * (len - 1) / 2;
18                 len = 1;
19             } else {
20                 len++;
21             }
22         }
23         if (len > 1) res += len * (len - 1) / 2;
24         return res;
25     }
26 };

 

 1 int numberOfArithmeticSlices(int* A, int ASize) {
 2     int ending = 1;
 3     int cnt = 0;
 4     if(ASize < 3) return 0;
 5 
 6     for (int i = 1; i < ASize-1; ++i) {
 7         if(A[i]-A[i-1] == A[i+1]-A[i]){
 8             cnt += ending;
 9             ++ending;
10         }else {
11             ending = 1;
12         }
13     }
14     return cnt;
15 }

 

 

递归:

int sum = 0;
int slices(int *A, int i) {
    if (i < 2)
        return 0;
    int ap = 0;
    if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
        ap = 1 + slices(A, i - 1);
        sum += ap;
    } else
        slices(A, i - 1);
    return ap;
}

int numberOfArithmeticSlices(int* A, int ASize) {
    sum = 0;
    slices(A, ASize - 1);
    return sum;
    
}

 

转载于:https://www.cnblogs.com/guxuanqing/p/5971656.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值