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. git shell 中文

    alias ls="ls --show-control-chars" alias ll="ls -l"

  2. var 和 dynamic在实际项目中的应用

    先回顾一下这两个关键词的用法. var是个语法糖,是在用var声明变量的那一刻就确定了其变量的类型. 因为需要在声明的时候就确定其类型,所以要求在用var声明隐式局部变量的时候必须初始化该变量. 编译 ...

  3. Codis集群的搭建与使用

    一.简介 Codis是一个分布式的Redis解决方案,对于上层的应用来说,连接Codis Proxy和连接原生的Redis Server没有明显的区别(不支持的命令列表),上层应用可以像使用单机的Re ...

  4. String、StringBuilder

    public class testString{ public static void main(String[] args) { String a="cool"; String ...

  5. Codeforces Round #336 (Div. 2) D. Zuma 区间dp

    D. Zuma   Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gems ...

  6. 【poj2891-Strange Way to Express Integers】拓展欧几里得-同余方程组

    http://poj.org/problem?id=2891 题意:与中国剩余定理不同,p%ai=bi,此处的ai(i=1 2 3 ……)是不一定互质的,所以要用到的是同余方程组,在网上看到有人称为拓 ...

  7. 第一个React程序HelloWorld

    一.程序步骤 1.用React.createClass生成组件 2.调用React.render把组件渲染到页面中,dom的操作由react自动完成 二.代码 <!DOCTYPE html> ...

  8. outlook圆角table

    <table cellpadding="0" cellspacing="0" border="0" width="800&q ...

  9. 将web项目deploy到tomcat的方法

    如果已经把整个项目发布到tomcat的webapps文件夹下,就不用再配置tomcat的server.xml了(也就是不用配置<Context>节点) 并且,你的项目的WEB-INF/li ...

  10. Myeclipse2014配置JSF环境

    首先创建一个普通的webproject,然后看官网教程喽 https://www.genuitec.com/products/myeclipse/learning-center/web/myeclip ...