LN : leetcode 413 Arithmetic Slices
lc 413 Arithmetic Slices
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.
规律公式 Accepted
这个问题就是找有几个长度大于等于3的等差数列,我们知道如果有一个等差数列长为n,可将其裂为(n-1)*(n-2)/2个长度大于等于3的子数列。
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int len = 2, sum = 0;
for (int i = 2; i < A.size(); i++) {
if (A[i]+A[i-2] == 2*A[i-1]) {
len++;
} else {
if (len >= 3) sum += (len-1)*(len-2)/2;
len = 2;
}
}
if (len >= 3) sum += (len-1)*(len-2)/2;
return sum;
}
};
DP Accepted
如果用dp[i]表示到i位置为止的算数切片的个数,若能与之前两个数构成等差数列,则dp[i] = dp[i-1]+1,累加所有的dp[i]即可得到答案。
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
vector<int> dp(A.size(), 0);
int ans = 0;
for (int i = 2; i < A.size(); i++) {
if (A[i]+A[i-2] == 2*A[i-1]) dp[i] = dp[i-1]+1;
ans += dp[i];
}
return ans;
}
};
LN : leetcode 413 Arithmetic Slices的更多相关文章
- LeetCode 413 Arithmetic Slices详解
这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...
- [LeetCode]413 Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- 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 ...
- Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力)
Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力) 题目描述 如果一个数组1.至少三个元素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 ...
- 【LeetCode】413. Arithmetic Slices 等差数列划分
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 双指针 递归 动态规划 日期 题目地址:htt ...
- 【Leetcode】413. Arithmetic Slices
Description A sequence of number is called arithmetic if it consists of at least three elements and ...
- 413. Arithmetic Slices
/**************************Sorry. We do not have enough accepted submissions.*********************** ...
- LeetCode 446. Arithmetic Slices II - Subsequence
原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is ...
随机推荐
- 配置server禁止全部非法域名 訪问自己的server
1.Apache2.4.1曾经: 第一种 直接拒绝訪问 打开 httpd.conf 文件,将一下配置追加到文件最后. #直接拒绝全部非法域名 <VirtualHost *:80> Ser ...
- 【MongoDB】The description of procedure in MongoDB
In this blog the procedure of mongodb will be described in details. It is known that mongodb has pro ...
- form标签中id和name属性的区别
HTML元素的ID和Name属性的区别 一直认为ID和NAME是一样的,两个又可以一起出现,甚是疑惑. 今天BAIDU了一下,才发现里面大有文章.发出来研究研究: 最classical的答案:ID就像 ...
- wget和curl
1 curl比wget支持更多的协议 2 wget是支持递归的,而curl不支持
- Revit插件开发HelloWorld
1. 使用 VS2012 先建立一个项目. 2. 在这里我们选择建立C# 类库项目, 改动项目名称为HelloWorld. 能够自己定义改动项目存放路径. 3. 加入 Revit 插件 API 的引用 ...
- Newtonsoft.Json.dll 反序列化JSON字符串
上一篇JSON博客<JSON入门级学习小结--JSON数据结构>中已对JSON做了简单介绍,JSON字符串数组数据样式大概是这样子的: 如今因为项目需求(asp.net web网站,前台向 ...
- ChartCtrl源码剖析之——CChartLegend类
CChartLegend类用来绘制每一个波形的描述信息,它处于该控件的区域,如下图所示: CChartLegend类的头文件. #if !defined(AFX_CHARTLEGEND_H__CD72 ...
- EF 6.0 The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. c#中的时间
在SQL server 中,有两种日期时间类型: 1.DateTime 它的范围是 1753-1-1 至 9999-12-31 2.DateTime2 它的满园是 0001-01-01 至 9999- ...
- 在使用react时的异步问题解决
在时用react时, 常常会出现在创建一个对象后, 对象还没有创建完成就被使用的异步问题, 介于这种问题, 一种解决方法就是使用Promise, 将需要被等待的那一步放到Promise中, Promi ...
- (转)IE内存泄露,iframe内存泄露造成的原因和解决方案
http://my.oschina.net/jsan/blog/11169 http://blog.csdn.net/tianma630/article/details/8502395 jQuery ...