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 ...
随机推荐
- Pytorch 四种边界填充方式(Padding)
1. 选用卷积之前填充(强烈建议) 小生非常推荐大家不再使用卷积所带的填充方式,虽然那种方式简单,但缺陷太多.① 不能根据自己的需要来决定上与下填充不等的边界,左右填充不等的边界:② 边界填充零容易出 ...
- Excel+Word:Jupyter
直接打开Excel,可以增改删,但如果只是查了?Jupyter Lab/Jupyter Notebook是件利器. 工作内容之一,是复制Excel的一条记录,姓名.身份证号.银行卡号,粘贴在Word的 ...
- (2)通信中为什么要进行AMC?
AMC,Adaptive Modulation and Coding,自适应调制与编码. 通信信号的传输环境是变化不定的,信道环境时好时差.在这种情景下,我们不可能按照固定的MCS进行信号发送.假如信 ...
- 网络编程采用HttpClient类更好
一般人网络编程普遍用HttpWebRequest,类似下面的实现.我也一般都这样实现 string result = string.Empty; HttpWebRequest request = (H ...
- XSS挑战之旅(通过看代码解题)
XSS 挑战之旅 level 1 没有什么过滤 payload: <script>alert(1)</script> level 2 php关键代码: echo "& ...
- C# 微信公众平台开发(5)--添加图文素材
微信公众平台开发 --添加素材 关于微信公众号素材管理,我们可以通过接口文档,了解基本详情:http://mp.weixin.qq.com/wiki/10/10ea5a44870f53d79449 ...
- spark机器学习从0到1逻辑斯蒂回归之(四)
逻辑斯蒂回归 一.概念 逻辑斯蒂回归(logistic regression)是统计学习中的经典分类方法,属于对数线性模型.logistic回归的因变量可以是二分类的,也可以是多分类的.logis ...
- 如何在Unity中画抛物线
using UnityEngine; using System.Collections; using System.Collections.Generic; [ExecuteInEditMode] p ...
- 6.1Go方法
第六章 Go方法 在第三章中讲解了struct,面向对象编程OOP已经是一个编程范式了,Go语言同样支持OOP开发. 一个对象就是一个变量,在这个对象中包含了一些方法,一个方法是一个和特殊类型关联的函 ...
- mac OS和win7笔记本实现文件共享
记录下macbook通过共享读取win7笔记本中文件的过程,条件是两台电脑处于同一无线网中 win7操作 点击网络图标--属性--更改高级共享设置 选择公用后选择以下选项:启动网络发现--启动文件和打 ...