当前位置:网站首页>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)
边栏推荐
- 快速使用 Jacoco 代码覆盖率统计
- Zsh shell adds automatic completion and syntax highlighting
- [2022 ciscn] replay of preliminary web topics
- C语言队列
- Thinkcmf6.0 installation tutorial
- 即刻报名|飞桨黑客马拉松第三期等你挑战
- [advanced digital IC Verification] command query method and common command interpretation of VCs tool
- php导出百万数据
- Problem solving: unable to connect to redis
- 【webrtc】m98 screen和window采集
猜你喜欢
2022 welder (elementary) judgment questions and online simulation examination
Sign up now | oar hacker marathon phase III, waiting for your challenge
PHP exports millions of data
3D reconstruction - stereo correction
Qt学习27 应用程序中的主窗口
[2022 ciscn] replay of preliminary web topics
探索干货篇!Apifox 建设思路
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
MySQL multi column index (composite index) features and usage scenarios
Wechat applet data binding multiple data
随机推荐
微信小程序基本组件使用介绍
[UTCTF2020]file header
Hands on deep learning (IV) -- convolutional neural network CNN
paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
[Stanford Jiwang cs144 project] lab3: tcpsender
太真实了,原来自己一直没有富裕起来是有原因的
Wechat applet data binding multiple data
JS quick start (I)
[UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
Technology cloud report: from robot to Cobot, human-computer integration is creating an era
Info | webrtc M97 update
【VHDL 并行语句执行】
Content of string
这5个摸鱼神器太火了!程序员:知道了快删!
Ansible
Chip information website Yite Chuangxin
[UVM practice] Chapter 2: a simple UVM verification platform (2) only driver verification platform
Record a stroke skin bone error of the skirt
The charm of SQL optimization! From 30248s to 0.001s
Linux server development, MySQL process control statement