LeetCode 446. Arithmetic Slices II - Subsequence

原创 2016年11月17日 10:13:14

欢迎移步到我的个人博客

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

题目要求

A sequence of numbers 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 sequences:

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 subsequence slice of that array is any sequence of integers (P0, P1, …, Pk) such that 0 ≤ P0 < P1 < … < Pk < N.

A subsequence slice (P0, P1, …, Pk) of array A is called arithmetic if the sequence A[P0], A[P1], …, A[Pk-1], A[Pk] is arithmetic. In particular, this means that k ≥ 2.

The function should return the number of arithmetic subsequence slices in the array A.

The input contains N integers. Every integer is in the range of -231 and 231-1 and 0 ≤ N ≤ 1000. The output is guaranteed to be less than 231-1.

Example:

Input: [2, 4, 6, 8, 10]

Output: 7

Explanation:
All arithmetic subsequence slices are:
[2,4,6]
[4,6,8]
[6,8,10]
[2,4,6,8]
[4,6,8,10]
[2,4,6,8,10]
[2,6,10]
Subscribe to see which companies asked this question

题意解析

这道题的含义就是找出数列中所有3个数以上的等差数列的数量。

解法分析

使用动态规划来解这道题。假设dp[i][j]表示以A[i]结尾的子序列(P0, P1, …, Pi)构成的数列,序列中的元素之差为j。而dp[i][j]=dp[k][j]>0?dp[k][j]+1:1,0<=i

解题代码

    public int numberOfArithmeticSlices(int[] A) {
        int re = 0;
        HashMap<Integer, HashMap<Long, Integer>> diffMaps = new HashMap<>();
        for (int i = 0; i < A.length; i++) {
            HashMap<Long, Integer> diffMap = new HashMap<>();
            diffMaps.put(i, diffMap);
            int num = A[i];
            for (int j = 0; j < i; j++) {
                if ((long) num - A[j] > Integer.MAX_VALUE)
                    continue;
                if ((long) num - A[j] < Integer.MIN_VALUE)
                    continue;
                long diff = (long) num - A[j];
                int count = diffMaps.get(j).getOrDefault(diff, 0);
                diffMaps.get(i).put(diff, diffMaps.get(i).getOrDefault(diff, 0) + count + 1);
                re += count;
            }
        }
        return re;
    }

LeetCode 446. Arithmetic Slices II - Subsequence

一道长得一副dp的样子的dp题。这道题难度不算特别大,因为看得出来肯定是dp题。因为,一个等差序列里面有好几个小的等差序列。 例如,2 4是一个等差序列,2 4 6是一个等差序列。 所以我们发现等...
  • u013007900
  • u013007900
  • 2016年11月24日 15:54
  • 643

LeetCode 446. Arithmetic Slices II - Subsequence

Problem Statement(Source) A sequence of numbers is called arithmetic if it consists of at least thre...
  • junchen1992
  • junchen1992
  • 2016年11月06日 10:30
  • 1993

Leetcode-446. Arithmetic Slices II - Subsequence

题目: A sequence of numbers is called arithmetic if it consists of at least three elements and if th...
  • caoyan_12727
  • caoyan_12727
  • 2016年12月06日 20:52
  • 514

LeetCode 446. Arithmetic Slices II - Subsequence

**问题描述:** A sequence of numbers is called arithmetic if it consists of at least three element...
  • u010370157
  • u010370157
  • 2017年06月28日 21:52
  • 67

动态规划/leetcode/直接推导递推公式

通过编写动态规划系列题目,拓宽思路  413 Arithmetic slices 等差数列切片 子数组(连续子序列) 题意:寻找一个数组的连续子数组,使得这个子数组是等差数列。请求出这种子数组的个数。...
  • m0_37693059
  • m0_37693059
  • 2017年09月02日 00:03
  • 273

446. Arithmetic Slices II - Subsequence

A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff...
  • wdlsjdl2
  • wdlsjdl2
  • 2016年11月13日 09:51
  • 187

446. Arithmetic Slices II - Subsequence***

A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff...
  • alwaystry
  • alwaystry
  • 2016年12月11日 12:43
  • 94

LeetCode 446. Arithmetic Slices II - Subsequence

LeetCode 446. Arithmetic Slices II - Subsequence
  • luzheqi
  • luzheqi
  • 2016年11月17日 10:13
  • 843

Interleaving String -- LeetCode

原题链接: http://oj.leetcode.com/problems/interleaving-string/  这是一道关于字符串操作的题目,要求是判断一个字符串能不能由两个字符串按照他们自...
  • linhuanmars
  • linhuanmars
  • 2014年04月29日 04:28
  • 9875

[LeetCode]446. Arithmetic Slices II - Subsequence

https://leetcode.com/problems/arithmetic-slices-ii-subsequence/#/description 找到乱序数组中所有gap相同的子序列个数...
  • gqk289
  • gqk289
  • 2017年03月15日 11:46
  • 136
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 446. Arithmetic Slices II - Subsequence
举报原因:
原因补充:

(最多只允许输入30个字)