arithmetic-slices-ii-subsequence(太难了)
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(太难了)的更多相关文章
- Arithmetic Slices II - Subsequence LT446
446. Arithmetic Slices II - Subsequence Hard A sequence of numbers is called arithmetic if it consis ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- LeetCode446. Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- LeetCode 446. Arithmetic Slices II - Subsequence
原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is ...
- Leetcode: Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [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 ...
- 446. Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- 446 Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
详见:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/description/ C++: class Solution { ...
- 第六周 Leetcode 446. Arithmetic Slices II - Subsequence (HARD)
Leetcode443 题意:给一个长度1000内的整数数列,求有多少个等差的子数列. 如 [2,4,6,8,10]有7个等差子数列. 想了一个O(n^2logn)的DP算法 DP[i][j]为 对于 ...
- [LeetCode] Arithmetic Slices 算数切片
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
随机推荐
- STMPClient 发送邮件显示 不允许使用邮件名称.
在.net 2.0,3.5, 针对某些邮箱(还不清楚是什么样的邮件) , 使用微软自带的DLL发送邮件会提示不允许使用邮件名称 .... 使用Jmail可以发送. 解决方案: 1. ...
- 用HAProxy和KeepAlived构建高可用的反向代理
用HAProxy和KeepAlived构建高可用的反向代理 用HAProxy和KeepAlived构建高可用的反向代理 前言对于访问量较大的网站来说,随着流量的增加单台服务器已经无法处理所有的请求 ...
- 三维云模拟Three.js
http://www.mrdoob.com/#/131/clouds http://www.webgl.com/2012/03/webgl-demo-clouds/ <!DOCTYPE html ...
- HDU4836 The Query on the Tree(树状数组&&LCA)
由于智力的问题,百度之星完全lu不动..开场看第一题根据题目给的条件我觉得一定是可以构造出来的,题目给的意思颇有鸽巢原理的感觉,于是觉得开场第一题应该就是智力构造题了,想了半个小时,发现完全想不动,于 ...
- Java框架----SSH整合回顾
1,新建工程,类型为Web Project,设置默认编码为UTF-8,并创建如下文件夹 1,Source Folder 1,src 项目源码 2,config 配置文件 3,test 单元测试 2,普 ...
- poj 2599 A funny game 博弈论
思路:无向图,走过的点不能在走.dfs搞定…… 再就是后继中有必败点的为必胜点! 代码如下: #include<iostream> #include<cstdio> #incl ...
- Eclipse Java EE 创建 Dynamic Web Project
1. 创建一个web工程,此处用eclipse创建(如果对创建web工程很熟悉,可以不看的,本文目的是做一个记录) 1) 打开新建工程对话框,选择Dynamic web Proje ...
- JS之事件(一)
事件:交互 异步监听,不是JS引擎监听的 一.绑定 1.ele.onxxxx(eg:onclick) = function (e) { //回调函数/事件处理函数 } 兼容性很好,但同一个事件仅能绑定 ...
- asp.net dataTable添加列
DataTable dtNew = new DataTable(); dtNew.Columns.Add("ItemNo");//序列号列 dtNew.Columns.Add(&q ...
- lintcode:将二叉查找树转换成双链表
题目 将一个二叉查找树按照中序遍历转换成双向链表 给定一个二叉查找树: 4 / \ 2 5 / \ 1 3 返回 1<->2<->3<->4<->5. ...