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

太难了。。。

package com.company;

import java.util.*;

// 终于找到原因了。还是我重复加了
// 比如 46 46 47 48 49 // 用累积记录过往节点记录,发现超时了
// 参考了Discuss
// https://discuss.leetcode.com/topic/66725/o-n-2-mle-tle-in-c-try-this-one-concise-and-fast // 其中 +1 这个地方 想了很久,终于想通了。意味着之前有一个长度为2的,会变成3.而为什么只加1就可以呢,因为用的是 last but one,
// 倒数第二个,帮忙记录最后一个可能的结果,而倒数第二个和前一个的连接,总是一对一的。。 // 实在太难了 class Solution {
public int numberOfArithmeticSlices(int[] A) {
// Need use Long to avoid gap exceeding
Map<Integer, Map<Long, Integer>> mp = new HashMap<>();
Set<Long> cSet = new HashSet<>();
for (int k: A) {
cSet.add((long)k);
} int ret = 0;
Map<Long, Integer> lastMp;
long gap;
int tmp;
int curRet; for (int i=0; i<A.length; i++) {
Map<Long, Integer> tmpMp = new HashMap<>(); for (int j=i-1; j>=0; j--) { gap = (long)A[i] - (long)A[j]; if (tmpMp.containsKey(gap)) {
curRet = tmpMp.get(gap);
}
else {
curRet = 0;
} lastMp = mp.get(j);
if (!lastMp.containsKey(gap)) {
tmp = 0;
}
else {
tmp = lastMp.get(gap);
ret += tmp;
} //System.out.printf("val %d gap %d\n", A[i], gap); if (cSet.contains(A[i] + gap)) {
tmpMp.put(gap, curRet + tmp + 1);
//System.out.printf("here val %d gap %d\n", A[i], gap);
}
} mp.put(i, tmpMp);
}
return ret;
}
} public class Main { public static void main(String[] args) throws InterruptedException { System.out.println("Hello!");
Solution solution = new Solution(); // Your Codec object will be instantiated and called as such:
int[] A = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int ret = solution.numberOfArithmeticSlices(A);
System.out.printf("ret:%d\n", ret); System.out.println(); } }

arithmetic-slices-ii-subsequence(太难了)的更多相关文章

  1. Arithmetic Slices II - Subsequence LT446

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

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

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

  3. LeetCode446. 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

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

  5. Leetcode: Arithmetic Slices II - Subsequence

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

  6. [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 ...

  7. 446. Arithmetic Slices II - Subsequence

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

  8. 446 Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    详见:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/description/ C++: class Solution { ...

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

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

  10. [LeetCode] Arithmetic Slices 算数切片

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

随机推荐

  1. draw call 的优化

    用一张贴图,renderstate都设置成一致,多个draw合并成一个

  2. 通过spring.net中的spring.caching CacheResult实现memcached缓存

    通过spring.net中的spring.caching CacheResult实现memcached缓存1.SpringMemcachedCache.cs2.APP.config3.Program. ...

  3. BZOJ 1071组队

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1071 题目很好,居然写了很久,题解找了真多: 主要两种做法: O(n^2lgn),通过优先 ...

  4. Grid分组汇总

    Ext.onReady(function () {                Ext.define('personInfo', {                    extend: 'Ext. ...

  5. 10个jQuery插件分享

    原文:http://www.shejidaren.com/10-jquery-plugins.html blur.js blur.js是一个很有意思的插件,它能实现像WIN7 AERO效果的JS插件, ...

  6. 创建MySQL存储过程示例

    创建MySQL存储过程是学习MySQL数据库必须要掌握的知识,下文对创建MySQL存储过程作了详细的介绍,供您参考学习. AD:2013大数据全球技术峰会课程PPT下载 下文将教您如何创建MySQL存 ...

  7. (转载) .NET2.0程序集无法在.net 4.0 中运行的解决方案

    首先在MSDN上看到 4.0 的更新日志中有如下这条: .NET Framework 4 不能自动使用自己的公共语言运行时版本来运行由 .NET Framework 早期版本生成的应用程序. 若要使用 ...

  8. lintcode: 左填充

    题目 实现一个leftpad库,如果不知道什么是leftpad可以看样例 样例 leftpad("foo", 5) >> " foo" leftpa ...

  9. Project Euler 110:Diophantine reciprocals II 丢番图倒数II

    Diophantine reciprocals II In the following equation x, y, and n are positive integers. For n = 4 th ...

  10. c# 委托 和 事件

    当初学C#的时候,没有完全吃透的,只能现在继续了...  欠老账.... http://www.cnblogs.com/chengxingliang/archive/2013/05/21/305191 ...