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. 7 - py面向对象一条龙服务

    Python从设计之初就已经是一门面向对象的语言,在python里所有东西皆是对象. 下面通过一个实例来说明什么是面向对象. 引子 你是一家公司的员工,公司现在要开发一款“人狗战争”的游戏,人狗战争肯 ...

  2. ZOJ 2386 容斥原理

    题意:给出n个数,和m(1<=m<=200 000 000),求1~M中能被这n个数其中任意一个数整除的个数: 分析:n范围很小,可以枚举选择被哪些数整除,被奇数个整数整除加m/这个n个数 ...

  3. NYOJ298 点的变换 【矩阵乘法经典】

    任意门:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=298 点的变换 时间限制:2000 ms  |  内存限制:65535 KB 难度:5 ...

  4. Python-time和datetime模块

    一.time模块 1.表示时间的三种方式 >>> import time >>> time.time() #当前时间戳 1509525556.8485825 > ...

  5. 【洛谷P1582】倒水

    倒水 题目链接 显然,2^x个杯子里的水可以倒在一个杯子里 所以我们可以贪心地每次将N中最大的2^x减掉 减k次(若中途已经为0,直接输出0) 若大于0,用最小的比N大的2^x减剩下的N,即为答案 # ...

  6. [luoguP4306][JSOI2010]连通数

    \[Yeasion\] \[Nein\] 其实我很奇怪为什么我的正解和输出\(N \times N\)的效果是一样的.....嗯,大概是\(RP\)问题吧.... 嗯首先来看一下题目: 题目描述: 度 ...

  7. jdbc连接各种数据库字符串

    oracle driverClass:oracle.jdbc.driver.OracleDriver url:jdbc:oracle:thin:@127.0.0.1:1521:dbname mysql ...

  8. 解决:Visual Studio 启动就报错退出

    Please open an administrative CMD window and navigate to C:\Program Files (x86)\Microsoft Visual Stu ...

  9. PL/SQL 数组的使用

    一.固定数组 1.在模式(schema)级创建VARRAY类型 语法: CREATE OR REPLACE TYPE varray_type_name IS VARRAY(n) OF <elem ...

  10. Beginning DirectX11 Game Programming

    DirectX11 or 10 made a big change comparing to DirectX9 The fixed-function pipeline was removed in D ...