Description

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.

Discuss

本题是一道动态规划题,可以从最常见的动态规划方法入手。原问题的求解可以转换到子问题的求解。以例子为例[1, 2, 3, 4],数组长度为4,最小长度为3(题目规定),假设[1, 2, 3]已经是满足题目要求的子序列,这时添加4进来,我们只需要判断新添加进来的4和子序列最后一位 3 的差值和子序列的间距差是否相等。如果相等,则满足要求,计数。时间复杂度较高,运行较慢。

看了网上别人的解法,使用的滑动窗口的思想。很简洁,很快。大神还是厉害啊,还需要努力学习啊!

Code 1

class Solution {
private static final int MAX = Integer.MAX_VALUE;
public int numberOfArithmeticSlices(int[] A) {
if (A == null || A.length < 3) { return 0; }
int n = A.length;
int[][] dp = new int[n][n];
//初始化数组
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i - j < 2) { dp[j][i] = MAX; }
}
} int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i - 2; j++) {
if (i - j == 2) {
dp[j][i] = checkSlices(A, j, i);
if (dp[j][i] != MAX) { count++; }
continue;
}
dp[j][i] = (dp[j][i - 1] != MAX && A[i] - A[i - 1] == dp[j][i - 1]) ? dp[j][i - 1] : MAX;
if (dp[j][i] != MAX) { count++; }
}
}
return count;
} public int checkSlices(int[] a, int left, int right) {
int gap = a[left + 1] - a[left];
int bb = a[right] - a[left + 1];
if (gap == bb) {
return bb;
}
return MAX;
}
}

Code 2

class Solution {
public int numberOfArithmeticSlices(int[] A) {
if (A == null || A.length < 3) { return 0; }
int res = 0;
for (int i = 0; i < A.length; i++) {
res = res + helper(A, i);
}
return res;
} private int helper(int[] a, int start) {
int index = start;
int count = 0; while (index < a.length - 2 && a[index + 2] - a[index + 1] == a[index + 1] - a[index]) {
index++;
count++;
}
return count;
}
}

【Leetcode】413. Arithmetic Slices的更多相关文章

  1. 【LeetCode】413. Arithmetic Slices 等差数列划分

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 双指针 递归 动态规划 日期 题目地址:htt ...

  2. LeetCode 413 Arithmetic Slices详解

    这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...

  3. LN : leetcode 413 Arithmetic Slices

    lc 413 Arithmetic Slices 413 Arithmetic Slices A sequence of number is called arithmetic if it consi ...

  4. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  5. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  6. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  7. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  8. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  9. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

随机推荐

  1. 一张图看懂微软Power BI系列组件

    一.Power BI简介 Power BI是微软最新的商业智能(BI)概念,它包含了一系列的组件和工具.话不多说,直接上图吧: Power BI的核心理念就是让我们用户不需要强大的技术背景,只需要掌握 ...

  2. STM32-F429ZIT6-开发流程

    ---恢复内容开始--- 一.开发环境搭建 1.编译器安装破解 2.STM32Cube MX安装 3.驱动安装 4.固件库安装 二.硬件准备 1.PC 2.STM32开发板 3.下载线 三.资料准备 ...

  3. ABAP git客户端

    Jerry习惯把自己写的小程序放到自己的github上:https://github.com/i042416 对于写的ABAP程序,需要先把SAPGUI里的代码手动拷贝到本地,然后用git客户端pus ...

  4. python入门19 异常及异常处理 异常捕获

    常见异常举例 """ 一些异常举例:""" '''模块不存在 ModuleNotFoundError: No module named 'd ...

  5. 03_P52 课后作业

    1. 软件开发的早期阶段为什么进行可行性研究?应该从哪些方面研究系统的可行性? 1.进行可行性研究是为了该软件项目是否值得开发?是否具有经济效益?是否违反法律道德?是否技术可以实现?是否风险性高? 2 ...

  6. MySQL:数据库入门篇4

    1. 视图 创建视图 create view 视图名字 as 查询sql语句; drop view 视图名字; alter view 视图名字 as 查询sql语句; 2. 触发器 1. 插入事件触发 ...

  7. 【转】Android 之ActivityThead、ActivityManagerService 与activity的管理和创建

    在android中,Activity是四大组件中比较重要的一个(当然其他的也比较重要),那么android中是怎样管理这些activity的?应用的进程和主线程是怎么创建的,应用的消息循环又是在什么时 ...

  8. MVC学习六:Razor布局视图之【/Views/Shared/_Layout.cshtml】

    _Layout代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...

  9. 【洛谷P3853】 [TJOI2007]路标设置

    路标设置 题目链接 此题和跳石头很相似,都是二分答案,模拟判断是否可行 #include<iostream> #include<cstdio> using namespace ...

  10. 【luogu P2024 食物链】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2024 摘吊打集训队的九日dalao一句话 关于带有多个相对集合的全集,我们可以多开几倍的空间.每一倍的元素表 ...