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):返回给定日历段的值 ...
随机推荐
- 包装类的使用与Junit单元测试类
包装类: 针对八种基本数据类型定义相应的引用类型,使之有了类的特点,就可以调用类的方法 基本数据类型 包装类 boolean Boolean byte Byte short Short int Int ...
- 01Java核心-冷门知识001-包
1)导入静态方法和静态域 import 可以加上static关键字,导入静态的方法和静态域. 例如: package com.gail.test; import static java.lang.Sy ...
- Alpha冲刺 —— 5.4
这个作业属于哪个课程 软件工程 这个作业要求在哪里 团队作业第五次--Alpha冲刺 这个作业的目标 Alpha冲刺 作业正文 正文 github链接 项目地址 其他参考文献 无 一.会议内容 1.展 ...
- Java实现 洛谷 P1064 金明的预算方案
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元 ...
- Java实现 蓝桥杯 算法提高 快乐司机
算法提高 快乐司机 时间限制:1.0s 内存限制:256.0MB 问题描述 "嘟嘟嘟嘟嘟嘟 喇叭响 我是汽车小司机 我是小司机 我为祖国运输忙 运输忙" 这是儿歌"快乐的 ...
- 第四届蓝桥杯JavaB组省赛真题
解题代码部分来自网友,如果有不对的地方,欢迎各位大佬评论 题目1.世纪末星期 题目描述 曾有邪教称1999年12月31日是世界末日.当然该谣言已经不攻自破. 还有人称今后的某个世纪末的12月31日,如 ...
- kubernetes pod内抓包,telnet检查网络连接的几种方式
背景 在日常kubernetes的运维中,经常遇到pod的网络问题,如pod间网络不通,或者端口不通,更复杂的,需要在容器里面抓包分析才能定位.而kubertnets的场景,pod使用的镜像一般都是尽 ...
- set基本运用
/* set集合基本用法 */ #include<iostream> #include<set> using namespace std; //set<T>a; v ...
- java Exception 处理汇总
1.java.lang.Exception: No runnable methods 测试类,没有可以运行的方法 解决: 方法添加注释:@Test
- 【C#】AutoMapper 使用手册
目录 1 入门例子 2 注册 2.1 Profile 3 配置 3.1 命名约定 3.2 配置可见性 3.3 全局属性/字段过滤 3.4 识别前缀和后缀 3.5 替换字符 4 调用构造函数 5 数组和 ...