Java实现 LeetCode 729 我的日程安排表 I(二叉树)
729. 我的日程安排表 I
实现一个 MyCalendar 类来存放你的日程安排。如果要添加的时间内没有其他安排,则可以存储这个新的日程安排。
MyCalendar 有一个 book(int start, int end)方法。它意味着在 start 到 end 时间内增加一个日程安排,注意,这里的时间是半开区间,即 [start, end), 实数 x 的范围为, start <= x < end。
当两个日程安排有一些时间上的交叉时(例如两个日程安排都在同一时间内),就会产生重复预订。
每次调用 MyCalendar.book方法时,如果可以将日程安排成功添加到日历中而不会导致重复预订,返回 true。否则,返回 false 并且不要将该日程安排添加到日历中。
请按照以下步骤调用 MyCalendar 类: MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)
示例 1:
MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(15, 25); // returns false
MyCalendar.book(20, 30); // returns true
解释:
第一个日程安排可以添加到日历中. 第二个日程安排不能添加到日历中,因为时间 15 已经被第一个日程安排预定了。
第三个日程安排可以添加到日历中,因为第一个日程安排并不包含时间 20 。
说明:
每个测试用例,调用 MyCalendar.book 函数最多不超过 100次。
调用函数 MyCalendar.book(start, end)时, start 和 end 的取值范围为 [0, 10^9]。
class TNode{
int start;
int end;
TNode left;
TNode right;
TNode(int start, int end){
this.start = start;
this.end = end;
}
boolean insert (TNode node){
if (node.end <= this.start){
if (this.left == null){
this.left = node;
return true;
}
return this.left.insert(node);
}
else if (node.start >= this.end){
if (this.right == null){
this.right = node;
return true;
}
return this.right.insert(node);
}
else{
return false;
}
}
}
class MyCalendar {
TNode root;
public MyCalendar() {
root = null;
}
public boolean book(int start, int end) {
if (root == null){
root = new TNode(start, end);
return true;
}
return root.insert(new TNode(start, end));
}
}
/**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar obj = new MyCalendar();
* boolean param_1 = obj.book(start,end);
*/
Java实现 LeetCode 729 我的日程安排表 I(二叉树)的更多相关文章
- Java实现 LeetCode 732 我的日程安排表 III(暴力 || 二叉树)
732. 我的日程安排表 III 实现一个 MyCalendar 类来存放你的日程安排,你可以一直添加新的日程安排. MyCalendar 有一个 book(int start, int end)方法 ...
- Java实现 LeetCode 731 我的日程安排表 II(二叉树)
731. 我的日程安排表 II 实现一个 MyCalendar 类来存放你的日程安排.如果要添加的时间内不会导致三重预订时,则可以存储这个新的日程安排. MyCalendar 有一个 book(int ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- vue打包 报错问题记录
1. assetsPublicPath: '/' 修改为 assetsPublicPath: './' 2. untils.js里面增加 publicPath: '../../' 重新运行 ...
- AIRAC
AIRAC是国际上划定的,以28天为一个周期的航行资料定期生效制. 2020年比较特殊,共有14个周期,分别是: 2001 2020/01/02 2020/01/29 2002 2020/01/30 ...
- 使用naxsi
naxsi简介 naxsi 是一个nginx 防病毒,防跨站,sql 注入的一个模块.nginx的一个waf ,应用防火墙.非常好配置. naxsi 安装 naxsi 在debina/ubuntu 上 ...
- nodejs上使用sql
1.首先本地要安装mysql, https://www.mysql.com/downloads/. 2.在node中连接mysql,要安装mysql驱动,也就是npm安装mysql模块:npm i m ...
- mysql小白系列_12 sysbench
压测 1.查看机器负载load top - load average 1m 5m 15m cat /proc/loadavg 与CPU个数有关,1个load表示1个CPUcat /proc/cpuin ...
- MVC4.0接口学习
/// <summary> /// 正则验证身份证号是否合法 /// </summary> /// <param name="sIdCard"> ...
- postman发送请求携带Cookie
相关步骤: 1.下载 Postman-Interceptor_v0.2.24.zip插件 2.解压下载好的插件,将其拖到应用配置中 3.复制Postman-Interceptor_v中的id地址 4. ...
- dij+DP
众所周知,蒜蒜是一名热爱工作的好员工,他觉得时间就是金钱,做事情总是争分夺秒. 这天晚上,蒜蒜一个人去吃晚饭.不巧的是,吃完饭以后就开始下雨了,蒜蒜并没有带雨伞出来.但是蒜蒜热爱工作,工作使他快乐,他 ...
- python之Phantomjs无界面浏览器引擎自动化测试
文字搬运工,本文主要介绍PhantomJS功能,其中有屏幕快照功能,为后面更新[python接口自动化脚本更新版本],其中新版本中新增自动发送邮件功能正文带图片,使用PhantomJS截取图片后发送邮 ...
- Shone.Math开源系列2 — 实数类型(含分数和无理数)的实现
Shone.Math开源系列2 实数类型(含分数和无理数)的实现 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/ShoneSharp. 摘要: ...