原题链接: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. [JavaWeb基础] 028.CSS简介和基础语法

    css 概述 CSS 指层叠样式表 (Cascading Style Sheets) 样式定义如何显示 HTML 元素 样式通常存储在样式表中 把样式添加到 HTML 4.0 中,是为了解决内容与表现 ...

  2. java方式实现归并排序

    一.基本思想 归并排序是建立在归并操作上的一种排序算法,该算法是采用分治法的一个典型应用.具体操作如下:所谓的分治就是分而治之,以一分为二的原则,先把序列平均分解成二个左右子序列,然后递归左右二个子序 ...

  3. 【JVM】垃圾回收的四大算法

    GC垃圾回收 JVM大部分时候回收的都是新生代(伊甸区+幸存0区+幸存1区).按照回收的区域可以分成两种类型:Minor GC和Full GC(MajorGC). Minor GC:只针对新生代区域的 ...

  4. AUTOSAR-文档中所使用的UML文件

    https://mp.weixin.qq.com/s/OeUPNBVh1Vd_ZT1EZVKDZA   AUTOSAR官方对AUTOSAR的了解,自然比我们的了解多.在这样一个信息不对称的情况下,需要 ...

  5. 移动端fixed兼容问题

    最近做移动端页面,有个需求类似下图 底部导航用fixed定位时在部分iOS版本中会有问题: 1.上滑是底部会跟着滑动,手指松开时才会又回到底部 2.软键盘唤起的情况下,也会出现许多莫名其妙的问题 网上 ...

  6. Java实现 蓝桥杯 算法提高 套正方形(暴力)

    试题 算法提高 套正方形 问题描述 给定正方形边长width,如图按规律输出层层嵌套的正方形图形. 注意,为让选手方便观看,下图和样例输出均使用""代替空格,请选手输出的时候使用空 ...

  7. ASP.NET中IHttpHandler与IHttpModule的区别(带样例说明)

    IHttpModule相对来说,是一个网页的添加 IHttpHandler相对来说,却是网页的替换 先建一个HandlerDemo的类 using System; using System.Colle ...

  8. Java实现 LeetCode 509 斐波那契数

    509. 斐波那契数 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列.该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和.也就是: F(0) = 0, F(1) = 1 ...

  9. Java实现 蓝桥杯VIP 算法训练 判断字符位置

    判定字符位置 时间限制: 1Sec 内存限制: 128MB 提交: 487 解决: 251 题目描述 返回给定字符串s中元音字母的首次出现位置.英语元音字母只有'a'.'e'.'i'.'o'.'u'五 ...

  10. Linux 递归acl权限和默认acl权限

    递归acl权限 递归acl指给父目录设定acl时,所有的子文件和子目录都拥有相同的acl权限 setfacl -m u:boduo:rx -R /project/ 默认acl权限 默认acl权限的作用 ...