【Leetcode】413. Arithmetic Slices
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的更多相关文章
- 【LeetCode】413. Arithmetic Slices 等差数列划分
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 双指针 递归 动态规划 日期 题目地址:htt ...
- 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】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- Golang Gin 项目使用 Swagger
Golang Gin 项目使用 Swagger 标签(空格分隔): Go 首先需要github.com/swaggo/gin-swagger和github.com/swaggo/gin-swagger ...
- React学习笔记 - Hello World
React Learn Note 1 React学习笔记(一) 标签(空格分隔): React JavaScript 前.Hello World 1. 创建单页面应用 使用Create React A ...
- 【Hibernate那点事儿】—— Hibernate应该了解的知识
前言: 最近由于有点时间,就像深入的学习一下Hibernate.之前只是简单的使用,并没领会它的妙处.这里就趁着分享的机会,好好整理一下. 这篇主要讲到了下面几个部分: Hibernate框架 Hib ...
- Qt 窗口移动实现
很多人觉得系统自带的标题栏太丑了,想要自绘一个标题栏,去掉了系统自带的标题栏后,就需要自己实现窗口移动,下面的代码就是实现窗口移动. widget.h #ifndef WIDGET_H #define ...
- C++ 下使用curl 获取ftp文件
从http://curl.haxx.se/下载的win32版本的curl都不能使,#include <curl.h>后总是报错:external symbol ,意思就是没有链接到curl ...
- 中间件事务码R3AC1里Block Size的含义
在中间件事务码R3AC1可以为一个中间件的适配器对象维护Block size的大小. 以上图的尺寸为50为例,假设在ERP系统里有110个设备(equipment)需要下载,那么CRM中间件会自动生成 ...
- 确定浏览器是否支持某些DOM模块
var supportDOM2Core = document.implementation.hasFeature("Core","2.0"); var supp ...
- 云盘+Git GUI实现云盘文件版本号控制
以下介绍操作细节 1.先下载Git GUI 下载地址:http://msysgit.github.io/ 再下载百度云网盘 下载地址:http://pan.baidu.com 接下来就是安 ...
- Uva 11396 爪分解
题目链接:https://vjudge.net/contest/166461#problem/A 题意: 给定一个图,特点是每个点的度都是3,求是不是原图可以分解为全部鸡爪:每条边只属于一个鸡爪: 分 ...
- Cesium.js隐藏logo等信息
css: .cesium-widget-credits{ display:none!important;}js: var viewer = new Cesium.Viewer('cs', { anim ...