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 ...
随机推荐
- flex mxmlc 手动编译项目
首先: 1.下载flex的sdk,如果你电脑有装flash builder,它自带了一份,位于安装目录的sdks目录下. 备注:(sdk依赖java的jre) 2.配置mxmlc的java运行环境jr ...
- SecureCRT SSH VMware Ubuntu
Ubuntu 10.4 装完后网络OK. NAT模式下, 可以上网. 宿主机和客户机可以彼此ping通. 主要是检查SSH服务, 和防火墙是否关闭(UBUNTU 默认没有安装SSH, 默认启动了防火墙 ...
- 单片机与嵌入式 以及ARM DSP FPGA 几个概念的理解
嵌入式设备一般要满足实时性的要求,而实时性是要求数据输入和输出的延时满足一定的要求.当然嵌入式一般都便携性都比PC要好,功能没有PC多,PC是通用,他是专用,一般只专注某些功能的实现,比如DSP专注数 ...
- 使用多种客户端消费WCF RestFul服务(四)——Jquery篇
Jquery篇 互联网开发中少不了各类前端开发框架,其中JQUERY就是最流行之一,本篇我们就采用JQUERY来消费WCF RestFul服务,其中用到JSON基础知识,如果有想了解的朋友,请访问:& ...
- 经常在eclipse中导入web项目时,出现转不了项目类型的问题,导入后就是一个java项目。
1.在eclipse的项目上点右键,刷新项目.2.在项目上点右键,进入属性(properties)3.在左侧列表项目中点击选择“Project Facets”,在右侧选择“Dynamic Web Mo ...
- 实验 snort安装配置与规则编写
1 实验目的 在linux或windows任意一个平台下完成snort的安装,使snort工作在NIDS模式下,并编写符合相关情景要求的snort规则. 2 实验环境 物理机:windows 8.1 ...
- {$ecs_css_path}
includes里的init.php的187-194行 if (!empty($_CFG['stylename'])) { $smarty->assign('ecs_css_path', 'th ...
- Java学习-012-文件删除实例及源代码
此文源码主要为应用 Java 创建文件的源代码.若有不足之处,敬请大神指正,不胜感激! 文件删除源代码如下: /** * @function 文件操作:删除文件.若文件存在且未被占用,则删除文件:若文 ...
- php保存远程文件到本地的方法
用到了ob_start();<?php header("Content-type:text/html charset=utf-8"); if(!empty($_POST['p ...
- spring的cronExpression
CronExpression_CronExpression 规则 字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 ...