729. My Calendar I
原题链接:https://leetcode.com/problems/my-calendar-i/description/
我的答案
虽然自己实现出来了,但是没看懂这道题目考查的是什么?编程语言的熟练度?
import java.util.ArrayList;
import java.util.List;
/**
* Created by clearbug on 2018/3/17.
*/
public class MyCalendar {
private List<int[]> events;
public MyCalendar() {
events = new ArrayList<>();
}
public boolean book(int start, int end) {
for (int[] event : events) {
int eventStart = event[0];
int eventEnd = event[1];
if (end > eventStart && end <= eventEnd) {
return false;
}
if (start >= eventStart && start < eventEnd) {
return false;
}
if (start <= eventStart && end >= eventEnd) {
return false;
}
}
events.add(new int[]{start, end});
return true;
}
public static void main(String[] args) {
MyCalendar calendar = new MyCalendar();
// [null,true,true,false,false,true,false,true,true,true,false]
System.out.println(calendar.book(47, 50)); // true
System.out.println(calendar.book(33, 41)); // true
System.out.println(calendar.book(39, 45)); // false
System.out.println(calendar.book(33, 42)); // false
System.out.println(calendar.book(25, 32)); // true
System.out.println(calendar.book(26, 35)); // false
System.out.println(calendar.book(19, 25)); // true
System.out.println(calendar.book(3, 8)); // true
System.out.println(calendar.book(8, 13)); // true
System.out.println(calendar.book(18, 27)); // false
/**
* ["MyCalendar","book","book","book","book","book","book","book","book","book","book"]
[[],[47,50],[33,41],[39,45],[33,42],[25,32],[26,35],[19,25],[3,8],[8,13],[18,27]]
*/
}
}
既然没看懂它的含义,那就去看看官方答案吧!
官方答案一:简单暴力
官方答案一种使用了我不知道的德摩根定律,所以里面的判断条件要比我的简单的多:
import java.util.ArrayList;
import java.util.List;
/**
* Created by clearbug on 2018/3/17.
*/
public class MyCalendar {
private List<int[]> events;
public MyCalendar() {
events = new ArrayList<>();
}
public boolean book(int start, int end) {
for (int[] event : events) {
if (start < event[1] && end > event[0]) {
return false;
}
}
events.add(new int[]{start, end});
return true;
}
public static void main(String[] args) {
MyCalendar calendar = new MyCalendar();
// [null,true,true,false,false,true,false,true,true,true,false]
System.out.println(calendar.book(47, 50)); // true
System.out.println(calendar.book(33, 41)); // true
System.out.println(calendar.book(39, 45)); // false
System.out.println(calendar.book(33, 42)); // false
System.out.println(calendar.book(25, 32)); // true
System.out.println(calendar.book(26, 35)); // false
System.out.println(calendar.book(19, 25)); // true
System.out.println(calendar.book(3, 8)); // true
System.out.println(calendar.book(8, 13)); // true
System.out.println(calendar.book(18, 27)); // false
/**
* ["MyCalendar","book","book","book","book","book","book","book","book","book","book"]
[[],[47,50],[33,41],[39,45],[33,42],[25,32],[26,35],[19,25],[3,8],[8,13],[18,27]]
*/
}
}
官方答案二:使用 TreeMap
TreeMap 是红黑树的一种实现,红黑树这种东西本身我也不太懂,所以这里就先贴下代码吧:
import java.util.TreeMap;
/**
* Created by clearbug on 2018/3/17.
*/
public class MyCalendar {
TreeMap<Integer, Integer> calendar;
MyCalendar() {
calendar = new TreeMap();
}
public boolean book(int start, int end) {
Integer prev = calendar.floorKey(start),
next = calendar.ceilingKey(start);
if ((prev == null || calendar.get(prev) <= start) &&
(next == null || end <= next)) {
calendar.put(start, end);
return true;
}
return false;
}
public static void main(String[] args) {
MyCalendar calendar = new MyCalendar();
// [null,true,true,false,false,true,false,true,true,true,false]
System.out.println(calendar.book(47, 50)); // true
System.out.println(calendar.book(33, 41)); // true
System.out.println(calendar.book(39, 45)); // false
System.out.println(calendar.book(33, 42)); // false
System.out.println(calendar.book(25, 32)); // true
System.out.println(calendar.book(26, 35)); // false
System.out.println(calendar.book(19, 25)); // true
System.out.println(calendar.book(3, 8)); // true
System.out.println(calendar.book(8, 13)); // true
System.out.println(calendar.book(18, 27)); // false
/**
* ["MyCalendar","book","book","book","book","book","book","book","book","book","book"]
[[],[47,50],[33,41],[39,45],[33,42],[25,32],[26,35],[19,25],[3,8],[8,13],[18,27]]
*/
}
}
729. My Calendar I的更多相关文章
- 【LeetCode】729. My Calendar I 解题报告
[LeetCode]729. My Calendar I 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar- ...
- [LeetCode] 729. My Calendar I 731. My Calendar II 732. My Calendar III 题解
题目描述 MyCalendar主要实现一个功能就是插入指定起始结束时间的事件,对于重合的次数有要求. MyCalendar I要求任意两个事件不能有重叠的部分,如果插入这个事件会导致重合,则插入失败, ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- LeetCode All in One 题目讲解汇总(转...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
- Java 时间类-Calendar、Date、LocalDate/LocalTime
1.Date 类 java.util.Date是一个"万能接口",它包含日期.时间,还有毫秒数,如果你只想用java.util.Date存储日期,或者只存储时间,那么,只有你知道哪 ...
- Js: Extensible Calendar Examples
http://ext.ensible.comhttps://github.com/bmoeskau/Extensiblehttps://github.com/TeamupCom/extensibleh ...
- Calendar类
Calendar类 注意:根据日历规则,如果想要这个月减去5天,那么则为: add(Calendar.Day,-5) 成员方法: public int get(int field):返回给定日历段的值 ...
随机推荐
- [Python基础]010.os模块(2)
os模块(2) 介绍 os 常量 路径 判断路径属性 路径变换 文件属性 相同文件 介绍 - os.path模块,主要处理路径操作,包含了各种处理文件和文件名的方法. os.path 常量 os.pa ...
- 几种常见的app推广形式
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 金融SaaS平台之构思篇
背景介绍 从事过金融服务行业的同学们都知道,业务系统是非常之多的,核心的就有估值.交易.TA.资讯系统,其他类似产品生命周期系统.投后分析系统等待,而且各个业务系统的逻辑非常之复杂,所以一般金融机构都 ...
- Java实现 LeetCode 493 翻转对
493. 翻转对 给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对. 你需要返回给定数组中的重要翻转对的数 ...
- Java实现 LeetCode 38 外观数列
38. 外观数列 「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述.前五项如下: 1 11 21 1211 111221 1 被读作 "one 1" ...
- java实现三角螺旋阵
方阵的主对角线之上称为"上三角". 请你设计一个用于填充n阶方阵的上三角区域的程序.填充的规则是:使用1,2,3-.的自然数列,从左上角开始,按照顺时针方向螺旋填充. 例如:当n= ...
- Java实现第九届蓝桥杯全球变暖
全球变暖 题目描述 你有一张某海域NxN像素的照片,"."表示海洋."#"表示陆地,如下所示: ....... .##.... .##.... ....##. ...
- Python学习之求阶乘篇
描述 给定一个数n,范围为0≤n≤100,请你编程精确的求出n的阶乘n!. 输入 输入数据有多行,每行一个整数n,当n<0时输入结束. 输出 输出n的阶乘. 样例输入 1234-1 样例输出 1 ...
- python—异常处理
一:什么是异常? (异常就是程序运行时发生错误的信号) 错误分两种: 1.语法错误 2.逻辑错误 二:异常的种类? (在python中不同的异常可以用不同的类型(python中统一了类与类型,类型即类 ...
- 还在用SimpleDateFormat格式化时间?小心经理锤你
还在用SimpleDateFormat格式化时间?小心经理锤你 场景 本来开开心心的周末时光,线上突然就疯狂报错,以为程序炸了,截停日志,发现是就是类似下述一段错误 java.lang.NumberF ...