[LeetCode]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.
解题: 如果一个等差数列是长度是n,那么一共有一个 (n-1)(n-2)/2 个子等差数列,所以先求出所有最长的子等差数列的长度即可,利用滑动窗口,两个游标,当当前遇等差数列的条件不满足了以后
更新计数值和游标,代码如下:
public int numberOfArithmeticSlices(int[] A) {
if (A == null || A.length < 3) {
return 0;
} else {
int count = 0;
int left = 0, right = 1;
int diff = A[right] - A[left];
while (right < A.length) {
if (right < A.length - 1 && A[right + 1] - A[right] == diff) {
right++;
} else {
int length = right - left + 1;
if (length >= 3) {
count += ((1 + (length - 2)) * (length - 2)) / 2;
}
if (right == A.length - 1) {
break;
} else {
left = right;
right++;
diff = A[right] - A[left];
}
} }
return count;
}
}
[LeetCode]413 Arithmetic Slices的更多相关文章
- LeetCode 413 Arithmetic Slices详解
这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...
- LN : leetcode 413 Arithmetic Slices
lc 413 Arithmetic Slices 413 Arithmetic Slices A sequence of number is called arithmetic if it consi ...
- 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 ...
随机推荐
- 关于 The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. 异常处理
导入Excel在本地环境没有问题,但部署到服务器上后出现异常. 经排查,是系统问题,因为本地是32位系统,而服务器上则是64位系统. 解决方法: 1.打开IIS管理器 2.右击应用程序所在的连接池 3 ...
- 北邮oj 题
题目描述 Every year,prince prepares a birthday gift for princess.The gift is a box,which is decorated wi ...
- mysql从零开始
常用的数据库有哪些? oralce,sqlserver,mysql,db2 有钱就用oracle吧 oracle和mysql的区别:https://zhidao.baidu.com/question/ ...
- Pow 算法
#include <iostream> using namespace std; template<class T, class Int> T Pow(T x, Int n) ...
- 使用UDP协议与韩国OACIS压机通讯
最近一个项目需要发送SN给OACIS, 研究了一下OACIS文档, 从文档中可以看出,传输协议只能使用UDP,切传输命令为>SN1; + SN + ; + LF LF的定义在Linux和Uni ...
- springmvc 学习笔记_1
一.回顾JavaWeb中的MVC设计模式 1)MVC这种设计模式,不光运用于Web领域,而且也能用于非Web领域 2)今天说的MVC特指一种表现层设计模式,不限于Java语言 二.回顾struts2+ ...
- ORACLE 日常处理办法
Oracle删除当前用户下所有的表的方法 1.如果有删除用户的权限,则可以: drop user user_name cascade; 加了cascade就可以把用户连带的数据全部删掉. 删除后再创建 ...
- Unity 之 Shader 对Z深度的偏移
对Z深度的偏移 Offset 指令给了我们一个操作正常的ZTest 检测结果的手段. Offset有两个参数,这两个参数理解起来不是很直观,而且具体实现是和硬件相关的 下面在实际例子中看他的效果 Sh ...
- ubuntu新建用户无法登陆
使用sudo adduser 创建用户,不存在无法登陆问题,如果使用useradd创建用户xx,需要在新建home目录下建立用户目录. 同时,需要修改用户目录的属主,命令:chown xx:xx ...
- QlikView 权限设置问题和注意
企业级报表通常都涉及到复杂的权限问题, 比如文本级权限和行级权限,某区域经理只能看到该区域的销售数据.QlikView自然也提供了该种功能. 具体方法: 在Edit script中新建一个tab, 输 ...