当前位置:网站首页>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)

边栏推荐
- Numbers that appear only once
- C language flight booking system
- Redis technology leak detection and filling (II) - expired deletion strategy
- paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
- Force buckle 145 Binary Tree Postorder Traversal
- [Stanford Jiwang cs144 project] lab4: tcpconnection
- 2022 welder (elementary) judgment questions and online simulation examination
- Shell 脚本的替换功能实现
- Cnopendata list data of Chinese colleges and Universities
- 2022焊工(初级)判断题及在线模拟考试
猜你喜欢

Use and analysis of dot function in numpy

PHP exports millions of data

The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)

Thinkcmf6.0 installation tutorial

The charm of SQL optimization! From 30248s to 0.001s

padavan手动安装php
![[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)](/img/e1/9a047ef13299b94b5314ee6865ba26.png)
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)

2022焊工(初级)判断题及在线模拟考试

2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案

Qt学习28 主窗口中的工具栏
随机推荐
PHP exports millions of data
Leetcode 43 String multiplication (2022.02.12)
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
[webrtc] M98 screen and window acquisition
Quickly use Jacobo code coverage statistics
Common method signatures and meanings of Iterable, collection and list
Technology cloud report: from robot to Cobot, human-computer integration is creating an era
C语言队列
Who has docker to install MySQL locally?
Use and analysis of dot function in numpy
Binary tree and heap building in C language
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
C language queue
buuctf misc USB
【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)
Leanote private cloud note building
探索Cassandra的去中心化分布式架构
misc ez_ usb
Visualization Document Feb 12 16:42
[UTCTF2020]file header