当前位置:网站首页>LeetCode中等题之我的日程安排表 I
LeetCode中等题之我的日程安排表 I
2022-07-07 04:47:00 【·星辰大海】
题目
实现一个 MyCalendar 类来存放你的日程安排。如果要添加的日程安排不会造成 重复预订 ,则可以存储这个新的日程安排。
当两个日程安排有一些时间上的交叉时(例如两个日程安排都在同一时间内),就会产生 重复预订 。
日程可以用一对整数 start 和 end 表示,这里的时间是半开区间,即 [start, end), 实数 x 的范围为, start <= x < end 。
实现 MyCalendar 类:
MyCalendar() 初始化日历对象。
boolean book(int start, int end) 如果可以将日程安排成功添加到日历中而不会导致重复预订,返回 true 。否则,返回 false 并且不要将该日程安排添加到日历中。
示例:
输入:
[“MyCalendar”, “book”, “book”, “book”]
[[], [10, 20], [15, 25], [20, 30]]
输出:
[null, true, false, true]
解释:
MyCalendar myCalendar = new MyCalendar();
myCalendar.book(10, 20); // return True
myCalendar.book(15, 25); // return False ,这个日程安排不能添加到日历中,因为时间 15 已经被另一个日程安排预订了。
myCalendar.book(20, 30); // return True ,这个日程安排可以添加到日历中,因为第一个日程安排预订的每个时间都小于 20 ,且不包含时间 20 。
提示:
0 <= start < end <= 10^9
每个测试用例,调用 book 方法的次数最多不超过 1000 次。
来源:力扣(LeetCode)
解题思路
用一个最简单的方法来记录一下。
class MyCalendar:
def __init__(self):
self.record=[]
def book(self, start: int, end: int) -> bool:
if not self.record:
self.record.append([start,end])
return True
else:
for i in self.record:
if i[0]<end and i[1]>start:
return False
self.record.append([start,end])
return True
# Your MyCalendar object will be instantiated and called as such:
# obj = MyCalendar()
# param_1 = obj.book(start,end)

边栏推荐
- 2022年茶艺师(中级)考试试题及模拟考试
- 自定义类加载器加载网络Class
- CTF daily question day43 rsa5
- 追风赶月莫停留,平芜尽处是春山
- Who has docker to install MySQL locally?
- The principle and implementation of buffer playback of large video files
- Thinkcmf6.0安装教程
- 【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
- [2022 ciscn] replay of preliminary web topics
- 2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
猜你喜欢

Force buckle 144 Preorder traversal of binary tree

Technology cloud report: from robot to Cobot, human-computer integration is creating an era

Codeforce c.strange test and acwing
![[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替](/img/e3/cceede6babae3c8a24336c81d98aa7.jpg)
[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替

Cnopendata American Golden Globe Award winning data

Most elements

央视太暖心了,手把手教你写HR最喜欢的简历
![[UTCTF2020]file header](/img/e3/818e2d531a06ab90de189055f634ad.png)
[UTCTF2020]file header

JSON data flattening pd json_ normalize

2022焊工(初级)判断题及在线模拟考试
随机推荐
开源生态|打造活力开源社区,共建开源新生态!
Operation suggestions for today's spot Silver
Niu Mei's mathematical problem --- combinatorial number
青龙面板--花花阅读
Linux server development, MySQL stored procedures, functions and triggers
Zhilian + AV, AITO asked M7 to do more than ideal one
[CV] Wu Enda machine learning course notes | Chapter 8
The charm of SQL optimization! From 30248s to 0.001s
贝叶斯定律
Button wizard collection learning - mineral medicine collection and running map
[UVM foundation] what is transaction
[2022 ciscn] replay of preliminary web topics
央视太暖心了,手把手教你写HR最喜欢的简历
Content of string
2022制冷与空调设备运行操作复训题库及答案
Implementation of replacement function of shell script
芯片资料 网站 易特创芯
Chip design data download
Force buckle 144 Preorder traversal of binary tree
Button wizard script learning - about tmall grabbing red envelopes