原题链接: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. websocket ddos检测脚本

    from websocket import create_connection while(1): ws = create_connection('wss://ha-cmim.cmcc-cs.cn:1 ...

  2. 个人工具,编辑器visual studio code

    个人收集的使用方法:简化版 主要基于基础web前端开发,visual studio code教程——基础使用.扩展插件安装使用 下载地址: https://visualstudio.microsoft ...

  3. doReleaseShared源码分析及唤醒后继节点的过程分析

    文章结构 源码:对doReleaseShared()方法的源码进行一些注释 使用场景:介绍doReleaseShared()使用位置,及目的 以写锁开始的队列:分析写锁开始得同步等待队列在唤醒后续读锁 ...

  4. java实现硬币方案

    标题:硬币方案 有50枚硬币,可能包括4种类型:1元,5角,1角,5分. 已知总价值为20元.求各种硬币的数量. 比如:2,34,6,8 就是一种答案. 而 2,33,15,0 是另一个可能的答案,显 ...

  5. Linux RPM管理命令

    RPM安装 rpm -ivh 包全名,其中,-i 表示安装,-v 表示显示详细信息,-h 表示显示进度 手动安装会有很多包依赖需要解决,如果是模块依赖,可以使用rpmfind进行查询解决 RPM升级 ...

  6. 一个Redis查询案例

    1.远程登陆进服务器 使用ssh连接至Linux服务器中 2.接入redis集群 redis-cli -h 10.1.8.12 -p 29000 3.执行查询命令 根据userid查询用户的最近在线时 ...

  7. UVIYN MMDVM充电宝支持APRS与 YSF

    需求就是要在APRS地图上显示对讲机位置 1.打开pi-star首页链接配置的专家(EXPERT)设置 下面链接快速打开 http://ip/admin/expert/edit_ysfgateway. ...

  8. System.getProperty("user.dir")获取的到底是什么路径?

    一直用System.getProperty("user.dir")来获取文件目录,我在执行单个方法调试和执行测试脚本的时候碰到一个问题, 我写了一个类ElementInitiali ...

  9. lambda表达式操作DataTable

    using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text; ...

  10. Vue好书推荐

    1.Vue.js实战 从基础知识到ui组件封装和剖析,层层推进,最后两个案例实战.适合零基础入门,学完可就业.(推荐看这本) 交流地址(pdf原件):链接(点击跳转):提取码:7IsG 2.vue.j ...