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 ...
随机推荐
- P1090 合并果子
#include <bits/stdc++.h> using namespace std; const int maxn = 10005; int main(int argc, char ...
- [转]GPS纠偏算法,适用于google,高德体系的地图
此文是转的,算法没验证过,只是记录一下. GPS纠偏算法,适用于google,高德体系的地图,精确度还比较高.我试了一下比高德本身的纠偏还精确点. /** * gps纠偏算法,适用于google,高德 ...
- 设置myeclipse 项目编码(UTF-8)
设置myeclipse开发项目默认编码为UTF-8Window-->Preferences-->General-->Workspace-->(Text file encodin ...
- php面向对象之__toString()
似曾相识,在php面向对象编程之魔术方法__set,曾经介绍了什么是魔术方法,这一章又介绍一个魔术方法__tostring(). __toString()是快速获取对象的字符串信息的便捷方式,似乎魔术 ...
- mysql sql技巧篇
1.left join 需要注意的事项 以左表为基准,匹配右表,如果右表匹配了两条,那么,就生成两条记录,而这两条记录的坐表信息都是一样的. 之前误以为,右表不会影响记录的条数.select 部分,不 ...
- oracle communities
应该常来这看看 https://www.oracle.com/communities/index.html
- Nginx的配置中与流量分发相关的配置规范:
1.除首页外,其他页面都在某个目录中首页可以直接在根目录下,其他页面都要在根目录下的目录中.不同的location尽量使用第一个dir的模式进行区分,便于区分该流量是落在nginx本地,还是转发到后端 ...
- HDFS API 文件读写代码演示
一:准备工作 1.新建class类 2.开启HDFS服务 3.将配置文件拷贝进resources路径 方便了Configuration的读取配置. 二:读出HDFS文件系统中的文件到控制台 4.读出在 ...
- Android 开发-Intent传递普通字符串
假设A传递id到B中 ActivityA: Intent intent=new Intent(); intent.setClass(ActivityA.this,ActivityB.class) ...
- 【python】RGB图片到灰度图的转换
在做立体匹配求深度图的时候遇到这个问题,用惯了matlab的rgb2gray,倒是不熟悉python的实现,在网上找到了相关方案,记下来已作备用 RGB到灰度图转换公式: Y' = 0.299 R + ...