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 ...
随机推荐
- Spring Batch 读 10 万条记录,写到 MongoDB
实践内容 从 MariaDB 一张表内读 10 万条记录,经处理后写到 MongoDB . 具体实现 1.新建 Spring Boot 应用,依赖如下: <!-- Web 应用 --> & ...
- Openwrt:编译固件提示[mktplinkfw] error: images are too big 错误
在编译mr3420的固件时,添加了luci.jamvm,但是最终编译的固件"openwrt-ar71xx-generic-tl-mr3420-v1-squashfs-factory.bin& ...
- Git上传文件、文件夹到github
上传一个文件: $ git add test.txt 上传多个文件: $ git add test.txt demo.txt 同理: 上传一个文件夹: $ git add learngit 上传多个文 ...
- python解析excel中图片+提取图片
解析表格是常用的技术.但是有些表各里面有图片怎么办?我想获得表格里面的图片,值得注意的是,图片没有位置信息,所以最好给图片进行编号,编号代表位置. 下面附上提取表格里面图片的代码.只要输出表格地址,和 ...
- 开源电影项目源码案例重磅分析,一套代码发布小程序、APP平台多个平台
uni-app-Video GitHub地址:https://github.com/Tzlibai/uni-app-video 一个优秀的uni-app案例,旨在帮助大家更快的上手uni-app,共同 ...
- virtualbox复制了以后网卡启动不了。
还是有些坑..必须踩 virtualbox复制了以后网卡启动不了.原因是如果没选重新生成mac ,那么mac地址会和第一台 机器一模一样,出现ip 征用的情况. 第二是系统方面的原因,你要删一个文件, ...
- 案例 (一)如何把python项目部署到linux服务器上
一.背景 用Python写了个脚本,需要部署到Linux环境的服务器上,由于服务器linux系统(centos,redhat等)自带的是python2,现在的python萌新都是从python3开 ...
- npm WARN enoent ENOENT: no such file or directory
https://github.com/visionmedia/debug/issues/261
- __call__ 与__init__,object 参数的使用
class test1: ###有object是可读可写 def __init__(self): ##__init__表示构造函数.__call__是析构函数. self.__pravite = &q ...
- Deno会在短期内取代Node吗?
转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://blog.bitsrc.io/what-is-deno-and-will-it-r ...