439. Segment Tree Build II
最后更新
08-Jan-2017
开始介绍线段树的主要作用了,可以快速在区间查找极值,我猜是这样的。。。。。
一个NODE的最大值取决于它左边和右边最大值里大 按个,所以,所以什么?对了,我们该用post-order traversal来构建。
public class Solution {
public SegmentTreeNode build(int[] A) {
// write your code here
if (A.length == 0) return null;
return buildTree(A, 0, A.length - 1);
}
public SegmentTreeNode buildTree(int[] A, int left, int right) {
if (left > right) return null;
if (left == right) return new SegmentTreeNode(left, right, A[left]);
int mid = left + (right - left) / 2;
SegmentTreeNode leftChild = buildTree(A, left, mid);
SegmentTreeNode rightChild = buildTree(A, mid + 1, right);
SegmentTreeNode tempRoot = new SegmentTreeNode(left, right, Math.max(leftChild.max, rightChild.max));
tempRoot.left = leftChild;
tempRoot.right = rightChild;
return tempRoot;
}
}
439. Segment Tree Build II的更多相关文章
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- Segment Tree Build I & II
Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...
- lintcode :Segmemt Tree Build II
题目 Segmemt Tree Build II The structure of Segment Tree is a binary tree which each node has two attr ...
- Lintcode247 Segment Tree Query II solution 题解
[题目描述] For an array, we can build a Segment Tree for it, each node stores an extra attribute count t ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- Lintcode: Segment Tree Query II
For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...
- Lintcode: Segment Tree Build
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- 201. Segment Tree Build
最后更新 二刷 08-Jan-2017 一刷忘了什么时候做的,只是觉得这几个题挺好的,一步一步手动构建线段树,最终理解了这个数据结构,并且后面有利用的地方. 其实重要的3个东西题目已经提供了: 1) ...
- 247. Segment Tree Query II
最后更新 二刷 09-Jna-2017 利用线段树进行区间查找,重点还是如何判断每一层的覆盖区间,和覆盖去见与当前NODE值域的关系. public class Solution { public i ...
随机推荐
- ASP.NET MVC 学习4、Controller中添加SearchIndex页面,实现简单的查询功能
参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-method ...
- UVa 10883 (组合数 对数) Supermean
在纸上演算一下就能看出答案是:sum{ C(n-1, i) * a[i] / 2^(n-1) | 0 ≤ i ≤ n-1 } 组合数可以通过递推计算:C(n, k) = C(n, k-1) * (n- ...
- 安装IIS之后运行aspx 显示“服务器应用程序不可用” 解决办法
引起这个的原因大概是现安装了.Net Framework,后装的IIS导致.Net没有在IIS里注册. 另外,还有可能是ASPNET账户没有IIS所指定服务器目录的权限.在资源管理器中找到“工具-文 ...
- linux下useradd -p 添加用户并设定密码
useradd 有个参数-p是设定密码,我试过,不能登录,今天想到这个问题,搜索得:-p 后面跟的是密文,也就是经过加密之后的那个串. 附上网上的破解shadow口令的链接,都是牛人啊.. http: ...
- PHP最佳实践(译)
原文: PHP Best Practices-A short, practical guide for common and confusing PHP tasks 译者:youngsterxyf 最 ...
- JAVA模块化
今天转载JAVA模块化系列的三篇文章. 在过去几年,Java模块化一直是一个活跃的话题.从JSR 277(现已废止)到JSR 291,模块化看起来是Java进化过程中的必经一环.即便是基于JVM的未来 ...
- js基础学习第一天(关于DOM和BOM)一
关于BOM和DOM BOM 下面一幅图很好的说明了BOM和DOM的关系 BOM提供了一些访问窗口对象的一些方法,我们可以用它来移动窗口位置,改变窗口大小,打开新窗口和关闭窗口,弹出对话框,进行导航以及 ...
- ecshop 二次开发及模板标签
ecs_account_log // 用户账目日志表 ecs_activity // 活动表(代码,名称,开始,结束,描述) ecs_ad // 广告表(位置,类型,名称,链接,图片,开始,结 ...
- Oracle数据库中的违规策略规则的修正
如笔者计算机上违规的策略与规则: 为了安全,可如下方式对齐进行修正:
- Selenium IDE 测试
Selenium IDE 测试 调试是为了发现和修复测试脚本,任何脚本开发的共同步骤是错误的处理.为了使这一过程更加稳固,我们可以使用Selenium IDE的一个插件叫“Power Debugger ...