[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 ...
随机推荐
- Ajax1
一.Ajax是什么? 全称"Asynchronous JavaScript and XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJA ...
- cordova常用资料源
cordova的中文资料https://github.com/CordovaCn/CordovaCn 官方文档: http://cordova.apache.org/docs/en/latest/ E ...
- iOS基础-NSString及NSMutableString剖析
一.NSString头文件 NSString : NSObject 实现协议: NSCopying/NSMutableCopying/NSSecureCoding 类别: //扩展类别 NSStrin ...
- android添加第三方字体并设置的简单使用
1.java文件 package lpc.com.project006; import android.app.Activity; import android.content.res.AssetMa ...
- PostGIS ShapeFile 导入数据
系统环境:WIN10 相关软件:PostgresSQL+PostGIS+QGIS 1.添加postGIS数据扩展 (1).打开PostgresSQL 客户端pgAdmin,在需要导入shp数据的数据库 ...
- web程序员该学习什么
以我个人的观点分了几个级别,仅供参考 初级发展(学习期) 前端应该学习HTML javascript css 能够制造简单的前端页面满足自己的工作需求 后端应该学习asp.net or jsp or ...
- android隐藏底部虚拟键Navigation Bar实现全屏
隐藏底部虚拟键Navigation Bar实现全屏有两种情况 第一种:始终隐藏,触摸屏幕时也不出现 解决办法:同时设置以下两个参数 View.SYSTEM_UI_FLAG_HIDE_NAVIGATIO ...
- 博弈论揭示了深度学习的未来(译自:Game Theory Reveals the Future of Deep Learning)
Game Theory Reveals the Future of Deep Learning Carlos E. Perez Deep Learning Patterns, Methodology ...
- [z]Oracle性能优化-读懂执行计划
http://blog.csdn.net/lifetragedy/article/details/51320192 Oracle的执行计划 得到执行计划的方式 Autotrace例子 ...
- new(C# 参考)
在 C# 中,new 关键字可用作运算符.修饰符或约束. new 运算符 用于创建对象和调用构造函数. new 修饰符 用于隐藏基类中被继承的成员. new 约束 用于在泛型声明中约束可能用作类型参数 ...