Leetcode: 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.
A slice (P, Q)其实就是从P到Q的序列,(P,Q)是arithmetic slice要求是等差数列且至少有三个数,问array里面有多少个这样的(P, Q)
这道题难点在于想到用DP,一旦想到用DP,就迎刃而解
dp[i]表示以i结束的slice有多少个
public class Solution {
public int numberOfArithmeticSlices(int[] A) {
if (A==null || A.length<3) return 0;
int[] dp = new int[A.length];
int res = 0;
int prevDiff = A[1] - A[0];
for (int i=2; i<A.length; i++) {
if (A[i]-A[i-1] == prevDiff) {
dp[i] = dp[i-1]+1;
}
else dp[i] = 0;
res += dp[i];
prevDiff = A[i] - A[i-1];
}
return res;
}
}
Leetcode: Arithmetic Slices的更多相关文章
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Arithmetic Slices 算数切片
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- LeetCode——Arithmetic Slices
Question A sequence of number is called arithmetic if it consists of at least three elements and if ...
- Leetcode: Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- LeetCode 446. Arithmetic Slices II - Subsequence
原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is ...
- Leetcode之动态规划(DP)专题-413. 等差数列划分(Arithmetic Slices)
Leetcode之动态规划(DP)专题-413. 等差数列划分(Arithmetic Slices) 如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列. 例如,以下数列为 ...
- LeetCode 413 Arithmetic Slices详解
这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...
- 【LeetCode】413. Arithmetic Slices 等差数列划分
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 双指针 递归 动态规划 日期 题目地址:htt ...
- [LeetCode]413 Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
随机推荐
- Solr 连接数据库
实际工程应用中,从数据库导出数据创建索引再常见不过了,现在实验一下从数据库导入数据创建索引. 一.版本说明 Solr版本:4.7.0 数据库:sqlserver2005 二.配置步骤 1. 准备的j ...
- mysql的事务和select...for update
一.mysql的事务mysql的事务有两种方式:1.SET AUTOCOMMIT=0;也就是关闭了自动提交,那么任何commit或rollback语句都可以触发事务提交;如果SET AUTOCOMMI ...
- js之操作JSON数据
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...
- Ubuntu/Deepin下常用软件汇总(持续更新)
最近开始用Ubuntu了,好多软件都不是常用的了,在这边留底,以免忘记.如果没有写安装方法,则直接在软件源中可以找到 UNetbootin U盘制作工具,制作Ubuntu的安装U盘超好用 Braser ...
- 【转】NGUI版虚拟摇杆
http://blog.csdn.net/anyuanlzh/article/details/40107577 下面是我用nui实现的一个虚拟摇杆. 1,示图 2.代码如下,都有比较详细的注释,就不说 ...
- JAVA缓存技术之EhCache
最近再ITEYE上看到关于讨论JAVA缓存技术的帖子比较多,自己不懂,所以上网大概搜了下,找到一篇,暂作保存,后面如果有用到可以参考.此为转贴,帖子来处:http://cogipard.info/ar ...
- Java迭代 : Iterator和Iterable接口
从英文意思去理解 Iterable :故名思议,实现了这个接口的集合对象支持迭代,是可迭代的.able结尾的表示 能...样,可以做.... Iterator: 在英语中or 结尾是都是表示 .. ...
- rpc rmi http
1.RPC与RMI (1)RPC 跨语言,而 RMI只支持Java. (2)RMI 调用远程对象方法,允许方法返回 Java 对象以及基本数据类型,而RPC 不支持对象的概念,传送到 RPC 服务的消 ...
- QLineEdit 仿QQ签名框(思路很独特:用QSS::hover显示额外的图片)
今天鼓捣了半天,终于实现了自定义Qt中的QlineEdit控件的大致效果. 这个问题对于新手而言,主要有以下几个难点: 1.继承QLineEdit控件 2.QSS设置QLineEdit的相关样式,可以 ...
- QFile文件操作-QT
#include <QCoreApplication> #include<QFile> #include<QString> #include<QDebug&g ...