详见:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/description/

C++:

class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A)
{
int res = 0, n = A.size();
vector<unordered_map<int, int>> dp(n);
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < i; ++j)
{
long delta = (long)A[i] - A[j];
if (delta > INT_MAX || delta < INT_MIN)
{
continue;
}
int diff = (int)delta;
++dp[i][diff];
if (dp[j].count(diff))
{
res += dp[j][diff];
dp[i][diff] += dp[j][diff];
}
}
}
return res;
}
};

参考:https://www.cnblogs.com/grandyang/p/6057934.html

446 Arithmetic Slices II - Subsequence 算数切片之二 - 子序列的更多相关文章

  1. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  2. LeetCode 446. Arithmetic Slices II - Subsequence

    原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is ...

  3. 446. Arithmetic Slices II - Subsequence

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  4. 第六周 Leetcode 446. Arithmetic Slices II - Subsequence (HARD)

    Leetcode443 题意:给一个长度1000内的整数数列,求有多少个等差的子数列. 如 [2,4,6,8,10]有7个等差子数列. 想了一个O(n^2logn)的DP算法 DP[i][j]为 对于 ...

  5. Arithmetic Slices II - Subsequence LT446

    446. Arithmetic Slices II - Subsequence Hard A sequence of numbers is called arithmetic if it consis ...

  6. Leetcode: Arithmetic Slices II - Subsequence

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  7. [Swift]LeetCode446. 等差数列划分 II - 子序列 | Arithmetic Slices II - Subsequence

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  8. LeetCode446. Arithmetic Slices II - Subsequence

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  9. [LeetCode] Arithmetic Slices 算数切片

    A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...

随机推荐

  1. 微信小程序-setData()方法

    一般setData方法多用于点击后改变页面信息或者刷新后与后台交互获取最新的信息 注意: 直接修改 this.data 而不调用 this.setData 是无法改变页面的状态的,还会造成数据不一致 ...

  2. 分享一个纯css制作的动画化,在网页(手机)载入等的时候能够引用!

    CSS代码例如以下: /* Custom Stylesheet */ body, html { margin: 0; -webkit-font-smoothing: antialiased; back ...

  3. Nerv --- React IE8 兼容方案

    创建项目 创建一个目录,使用npm快速初始化 $ mkdir my-project && npm init -y 安装依赖 安装webpack以及babel $ npm install ...

  4. CodeForces 404 Marathon ( 浮点数取模 -- 模拟 )

    B. Marathon time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  5. PAT-PAT (Advanced Level) Practise 1001. A+B Format (20) 【二星级】

    题目链接:http://www.patest.cn/contests/pat-a-practise/1001 题面: 1001. A+B Format (20) Calculate a + b and ...

  6. MySQL 温故而知新--Innodb存储引擎中的锁

    近期碰到非常多锁问题.所以攻克了后,细致再去阅读了关于锁的书籍,整理例如以下:1,锁的种类 Innodb存储引擎实现了例如以下2种标准的行级锁: ? 共享锁(S lock),同意事务读取一行数据. ? ...

  7. SpringBoot项目报错Cannot determine embedded database driver class for database type NONE

    原因: Cannot determine embedded database driver class for database type NONE 这是因为spring boot默认会加载org.s ...

  8. Android 常用Shell命令

    1.查询模拟器/设备实例 adb devices 2.从模拟器/设备中拷入或拷出文件(默认拷贝在执行目录) 从模拟器或者设备中复制文件或目录,使用(如下命): adb pull <remote& ...

  9. JNI/NDK开发指南(2)

    1.生成动态库.so,存放于手机的system/lib/中(APP怎样将.so存入该文件夹,奇怪?????),Java层调用JNI的类会运行静态代码System.loadLibrary("* ...

  10. [IT学习]阿铭Linux 微信公众号 每日一题 解析

    1.shell习题171020公布的昨日答案 习题171019 - 打印正方形 #!/bin/bash read -p "please input a number:" sum a ...