[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 ...
随机推荐
- 设置centos7默认运行级别
1.查看当前运行级别 systemctl get-default 2.设置命令行运行级别 systemctl set-default multi-user.target 3.设置图形化运行级别 sys ...
- 转 Visual C++ 将整合Clang
原文见:http://www.solidot.org/story?sid=45898 微软在11月释出的Visual C++更新将整合Clang开源C和C++编译器,开发者将可以用Clang编译Win ...
- WCF在tcp通道下启用httpget
关于tcp通道下,启用httpget,必须启用一个http的基地址,如果要启用元数据交换,host中必须开启服务描述. //01 create host Uri tcpBaseAddress = ne ...
- sql查询上一条记录和下一条记录
上一条记录的SQL语句: * from news where newsid<id order by newsid DESC 下一条记录的SQL语句: * from news where news ...
- Hibernate SQL查询 addScalar()或addEntity()
本文完全引用自: http://www.cnblogs.com/chenyixue/p/5601285.html Hibernate除了支持HQL查询外,还支持原生SQL查询. 对原 ...
- form表单提交路径action="" 时的一种特殊情况
一.说明: 当页面的form表达的action=""时,表示表单会提交到当前页面,但是如果当前页面的URL里已经带有一个参数了,每次提交表达时这个参数依然存在,不管form表单里有 ...
- Pow 算法
#include <iostream> using namespace std; template<class T, class Int> T Pow(T x, Int n) ...
- centos从日志文件查找关键字的日志并生成文件
grep "unset user wechat user_id:" app* | tee wechat_log
- Hibernate一对一双向关联(注解)
每一个人(Person)对应一个身份证号(IdCard) package cqvie.yjq.domain; import java.util.Date; import javax.persisten ...
- 在Android开发中如何判读当前设备是否连接网络
1:前言: 我们在Android开发的过程中,很多实现是要向远程服务器拿数据的,但是未必当前设备一定连接了网络啊,那么此时我们就是要进行判断的了, 如果是有网络的话,那么此时就去向远程服务器去拿数据, ...