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. [工作积累] GCC 4.6 new[] operator内存对齐的BUG

    对于用户没有定义dctor(包括其所有成员)的类来说, new CLASS[n] 可能会直接请求sizeof(CLASS)*n的空间. 而带有dctor的 类, 因为delete[]的时候要逐个调用析 ...

  2. MVC 强类型视图

    MVC里面的强类型视图的确是一个很强大的东西,结合EF(Entity Framework)的话更加强大,可以直接把你的数据库直接生成强视图的增删查改的视图,在视图中所有Model的属性均动态的,我们不 ...

  3. <context:annotation-config> 跟 <context:component-scan>诠释及区别

    <context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注 ...

  4. 使用tomcat7创建异步servlet

    该篇文章翻译自:http://developerlife.com/tutorials/?p=1437 一.简介 Servlet API 3.0 之前,需要使用类似Comet的方式来实现创建异步的Ser ...

  5. 全注解的SSH框架

    基于struts2.23 + spring2.5.6 + hibernate3.6.4 + hibernate-generic-dao1.0(除了spring,我整合的都是最新的GA包,hiberna ...

  6. linq 常用语句

    自己练习的 switch (productDataAnalysisQuery.DataType) { : var data = (from hp in GPEcontext.hbl_product j ...

  7. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  8. Linux搜狗输入法在有道云笔记上输入冗余

    Linux下,在有道云笔记中用搜狗拼音输入法时,会出现输入冗余,类似于输入法的缓冲上屏了.这是有道云笔记Web页面的问题. 暂时的解决办法是按Ctrl + alt + p. 不仅搜狗输入法,凡是用fc ...

  9. TCP网络拥塞控制

    拥塞控制过程 数据吞吐量 TCP窗口大小,窗口流量控制,慢启动对TCP的成块数据传输综合作用,可能对TCP的数据传输有意想不到的影响. RTT(Round-Trip Time) :往返时间.是指一个报 ...

  10. jquery easyUi 配置默认页码

    jquery easyUI用pagenation 属性如果修改其默认加载页面显示,配置该怎样写? 注意区分datagrid的pagenation分页的区别,代码如下. if ($.fn.paginat ...