【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-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- WIN7系统程序放在中文文件夹打开报错及界面汉字变乱码
今天发现在一个服务商提供的设备的WIN7系统里,一个稳定运行的程序打开时报错,且界面汉字变乱码. 经测试发现程序放在英文名称的文件夹中可以正常打开,但界面上的汉字仍为乱码. 后检查“控制面板“--”区 ...
- May 13th 2017 Week 19th Saturday
Mountains look beautiful from a distance. 远处看山山更美. This gnomic seems to circulate very long, its mor ...
- LAMP环境基本配置
CentOS 7.0 LAMP环境搭建 Apache: 安装: yum -y install httpd 设为开机启动: systemctl start httpd.service systemctl ...
- ExtJS4 ajax请求同步异步问题
今天在写代码过程中遇到一个奇怪的问题.事情是这种,我写了一个简单的页面用来删除选中的用户,请看以下: 由于后台的servlet的代码实现了依据用户名来删除一条记录.所以我在前台的ExtJS代码里面用了 ...
- P3909 异或之积
P3909 异或之积 为什么叫做异或之积? 答曰:只要不关乎Alice和Bob就行 做完这道水题,感觉自己弱爆了. 一开始就要考虑暴力\(O(n^3)\)的优化. 然后就注意到了题目中的\(6\)为什 ...
- 【洛谷P1983】车站分级
车站分级 题目链接 首先,可以发现火车停靠站点的大小是没有什么规律的, 火车可以停靠在级别<=当前级别的站点,必须停靠在级别>=当前最高级别的站点 但是所有没有被停靠的站点级别一定比所有被 ...
- 1..net转java背景介绍
注册博客2年了.从注册就开始接触.net.以前也有想过转java.只是没想过会这么快. .net语法这么优美好用.可惜了生态环境. .net还没有学成大神.就要离开了. 公司有个项目要用java.在加 ...
- Angularjs实例2
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- html单选框(性别选择)
在写单选框时,如何实现只能同时只能选择一个radio. 将name设置为一样的数值:代码如下: <input class="myforms-3-2" type="r ...
- 2018-03-21 11:34:44 java脚本批量转换java utf-8 bom源码文件为utf-8编码文件
package com.springbootdubbo; import java.io.*;import java.util.ArrayList;import java.util.List; /** ...