247. Segment Tree Query II
最后更新
二刷
09-Jna-2017
利用线段树进行区间查找,重点还是如何判断每一层的覆盖区间,和覆盖去见与当前NODE值域的关系。
public class Solution {
public int query(SegmentTreeNode root, int start, int end) {
if (root == null) return 0;
if (start > root.end || end < root.start) return 0;
int coveredStart = Math.max(start, root.start);
int coveredEnd = Math.min(end, root.end);
if (root.start == coveredStart && root.end == coveredEnd) return root.count;
return query(root.left, coveredStart, coveredEnd) +
query(root.right, coveredStart, coveredEnd);
}
}
247. Segment Tree Query II的更多相关文章
- Lintcode: Segment Tree Query II
For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...
- 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 ...
- Segment Tree Query I & II
Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), ...
- Lintcode: Segment Tree Query
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- 202. Segment Tree Query
最后更新 二刷 09-Jan-17 正儿八经线段树的应用了. 查找区间内的值. 对于某一个Node,有这样的可能: 1)需要查找区间和当前NODE没有覆盖部分,那么直接回去就行了. 2)有覆盖的部分, ...
- 439. Segment Tree Build II
最后更新 08-Jan-2017 开始介绍线段树的主要作用了,可以快速在区间查找极值,我猜是这样的..... 一个NODE的最大值取决于它左边和右边最大值里大 按个,所以,所以什么?对了,我们该用po ...
- 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: Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
随机推荐
- JS concat() 方法
[数组元素的合并] 一. concat() 方法 concat() 方法用于连接两个或多个数组. 返回一个新的数组.该数组是通过把所有 arrayX 参数添加到 arrayObject 中生成的.如果 ...
- java生成随机字符
1.生成的字符串每个位置都有可能是str中的一个字母或数字,需要导入的包是import java.util.Random; //length用户要求产生字符串的长度 public static Str ...
- Linux中一些约定俗成的文件扩展名
注:Linux中的所有内容均以文件的形式保存,但不依靠扩展名区分文件类型(根据权限区分),约定俗成的文件扩展名是为了方便管理员对文件进行区分 压缩包:“*.gz”.“*.bz2”.“*.tar.bz2 ...
- 自制操作系统小样例——参考部分linux0.11内核源码
详细代码戳这里. 一.启动引导 采用软件grub2进行引导,基于规范multiboot2进行启动引导加载.multiboot2的文档资料戳这里. 二.具体内容 开发环境 系统环境:Ubuntu 14. ...
- windows本机域名配置
路径: C:\Windows\System32\drivers\etc打开hosts文件如下: # Copyright (c) - Microsoft Corp. # # This is a samp ...
- perl学习之:use and require
本文和大家重点学习一下Perl use和require用法对比,这两个函数都是一个意思,加载和引用Perl的模块,或者是子程序,区别在于Perl use是在当前默认的里面去寻找,一旦模块不在指定的区域 ...
- Python_编程题集_002_菱形
2.编写程序实现: n=5,输出: * *** ***** *** * n=6,输出: * *** ***** ***** *** * n为任意大于1的正整数. 解: #思路: # 第一步:判断行数, ...
- Images for Journals
Images for publication Table of Contents 1. Images for publication 1.1. image format : vector image ...
- javascript:与获取鼠标位置有关的属性
javascript并没有mouse对象,获取鼠标坐标要靠强大的event对象。 我们通过监听document的mousemove,就可以实时获得鼠标位置。 但是!!event中和鼠标相关的属性太多了 ...
- PC硬件以及引导加载器
PC 硬件 本文介绍供 x86 运行的个人计算机(PC)硬件平台. PC 是指遵守一定工业标准的计算机,它的目标是使得不同厂家生产的机器都能够运行一定范围内的软件.这些标准随时时间迁移不断变化,因此9 ...