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 ...
随机推荐
- sparse-PCA(稀疏主成分分析)是什么?
不多说,直接上干货! 复杂降维技术有spare-PCA和sparse coding. 最近在科研需要,感谢下面的博主. Sparse PCA 稀疏主成分分析
- 嵌入式开发之davinci--- 8148/8168/8127 中的图像缩放sclr、swms之后出现图像视频卡顿、屏幕跳跃的问题
()问题原因 这边的case链路是这样的camera->sclr(yuv420sp cif)->dup->ipcframeoutm3<->ipcframerocess&l ...
- c/c++内存使用原则
1 no malloc no free 2 no new no delete 如果对象不是new出来的,那么这个对象在生命周期结束后会自动调用析构函数自己释放自己的内存,不需要delete. 但是如果 ...
- Entity FramWork Code first 使用心得
1 最有用的命令 update-database -force -verbose 2 主键如果不是默认的int或者 bigint而是guid 或者 string类型,创建记录的时候要给主键赋值 3 在 ...
- java定时器2-spring实现
spring定时器(基于xml) spring定时器(基于注解) quartz定时器 1.使用基于xml配置的spring定时器 首先编写定时任务类Mytask public class Mytask ...
- 集成到Buildroot中的Helloword程序【转】
本文转载自:http://www.openloongson.org/forum.php?mod=viewthread&tid=85 本帖最后由 gt945 于 2015-7-5 17:06 编 ...
- jquery 中cache为true与false 的区别
$.ajax({type: "get",cache: false,url: url,success: function (msg) { }}); cache为true与false ...
- RDD基础
RDD的两种操作 1.Tansformation(转化操作):返回值还是一个RDD 2.Action(行动操作):返回值不是一个RDD 第一种Transformation是返回一个新的RDD ...
- WebBrowse使用
C# WinForm开发系列 - WebBrowser 2009-12-14 14:19:21 标签:C# - WebBrowser 休闲 WinForm开发系列 介绍Vs 2005中带的WebBr ...
- bzoj4276
线段树优化建图+费用流 朴素的做法是每个强盗直接对每个区间的每个点连边,然后跑最大权匹配,这样有5000*5000条边,肯定过不去,那么我们用线段树优化一下,因为线段树能把一个O(n)的区间划分为O( ...