原题链接: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的更多相关文章

  1. 【LeetCode】729. My Calendar I 解题报告

    [LeetCode]729. My Calendar I 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar- ...

  2. [LeetCode] 729. My Calendar I 731. My Calendar II 732. My Calendar III 题解

    题目描述 MyCalendar主要实现一个功能就是插入指定起始结束时间的事件,对于重合的次数有要求. MyCalendar I要求任意两个事件不能有重叠的部分,如果插入这个事件会导致重合,则插入失败, ...

  3. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  4. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

  5. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  6. LeetCode All in One 题目讲解汇总(转...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...

  7. Java 时间类-Calendar、Date、LocalDate/LocalTime

    1.Date 类 java.util.Date是一个"万能接口",它包含日期.时间,还有毫秒数,如果你只想用java.util.Date存储日期,或者只存储时间,那么,只有你知道哪 ...

  8. Js: Extensible Calendar Examples

    http://ext.ensible.comhttps://github.com/bmoeskau/Extensiblehttps://github.com/TeamupCom/extensibleh ...

  9. Calendar类

    Calendar类 注意:根据日历规则,如果想要这个月减去5天,那么则为: add(Calendar.Day,-5) 成员方法: public int get(int field):返回给定日历段的值 ...

随机推荐

  1. 02 . Prometheus告警处理

    Prometheus告警简介 告警能力在Prometheus的架构中被划分成两个独立的部分.如下所示,通过在Prometheus中定义AlertRule(告警规则),Prometheus会周期性的对告 ...

  2. Rocket - debug - TLDebugModuleInner - Abstract Command Decoding & Generation

    https://mp.weixin.qq.com/s/0zKSTktxgzo5uCUphqaWSQ 介绍抽象命令的解码和生成. 1. accessRegisterCommandReg accessRe ...

  3. Rocket - debug - TLDebugModuleInner - DMSTATUS

    https://mp.weixin.qq.com/s/GyGriFyeq_7Z3xOjKn56Mg 简单介绍TLDebugModuleInner中DMSTATUS寄存器的实现. 1. DMSTATUS ...

  4. SQL Server账号密码(sa)登录失败 错误原因:233

    (其实以前经常用的时候,都很简单,最近一段时间不用了,再一看发现都忘记的差不多了,还是写一篇博客吧,防止下一次再在这种问题上面浪费时间) 右键此电脑,点击管理 如果没有此电脑打开文件夹 在这里右键也是 ...

  5. Java实现字符串匹配

    1 问题描述 给定一个n个字符组成的串(称为文本),一个m(m <= n)的串(称为模式),从文本中寻找匹配模式的子串. 2 解决方案 2.1 蛮力法 package com.liuzhen.c ...

  6. java实现取球博弈

    今盒子里有n个小球,A.B两人轮流从盒中取球,每个人都可以看到另一个人取了多少个,也可以看到盒中还剩下多少个,并且两人都很聪明,不会做出错误的判断. 我们约定: 每个人从盒子中取出的球的数目必须是:1 ...

  7. java实现第四届蓝桥杯金蝉素数

    金蝉素数 考古发现某古墓石碑上刻着一个数字:13597,后研究发现: 这是一个素数! 并且,去掉首尾数字仍是素数! 并且,最中间的数字也是素数! 这样特征的数字还有哪些呢?通过以下程序的帮助可以轻松解 ...

  8. java实现第七届蓝桥杯抽签

    抽签 抽签 X星球要派出一个5人组成的观察团前往W星. 其中: A国最多可以派出4人. B国最多可以派出2人. C国最多可以派出2人. .... 那么最终派往W星的观察团会有多少种国别的不同组合呢? ...

  9. 转载:windows下安装mac虚拟机(Vmvare+mac)

    体验Mac的高效与思想,每个技术人都应该去了解和体验,本文转载自网络,使用Vmvare,虚拟Mac系统 https://blog.csdn.net/qq_31867709/article/detail ...

  10. Mysql添加索引及索引的优缺点

    一.什么是索引? 索引是对数据库表中的一列或多列值进行排序的一种结构,使用索引可以快速访问数据库表中的特定信息. 二.索引的作用? 索引相当于图书上的目录,可以根据目录上的页码快速找到所需的内容,提高 ...