博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
729. My Calendar I
阅读量:5098 次
发布时间:2019-06-13

本文共 4847 字,大约阅读时间需要 16 分钟。

原题链接:

我的答案

虽然自己实现出来了,但是没看懂这道题目考查的是什么?编程语言的熟练度?

import java.util.ArrayList;import java.util.List;/** * Created by clearbug on 2018/3/17. */public class MyCalendar {    private List
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
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
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]] */ }}

转载于:https://www.cnblogs.com/optor/p/8588969.html

你可能感兴趣的文章
罗马数字与阿拉伯数字转换
查看>>
Eclipse 反编译之 JadClipse
查看>>
asp.net 获取IP地理位置的几个主要接口
查看>>
Python入门-函数
查看>>
[HDU5727]Necklace(二分图最大匹配,枚举)
查看>>
距离公式汇总以及Python实现
查看>>
设计模式之装饰者模式
查看>>
一道不知道哪里来的容斥题
查看>>
Blender Python UV 学习
查看>>
window添加右键菜单
查看>>
入手腾龙SP AF90mm MACRO
查看>>
ORACLE 递归查询
查看>>
[Android] 开发第十天
查看>>
操作~拷贝clone()
查看>>
jQuery源码分析(2) - 为什么不用new jQuery而是用$()
查看>>
[转]【EL表达式】11个内置对象(用的少) & EL执行表达式
查看>>
ArrayList对象声明& arrayList.size()
查看>>
并发编程 线程
查看>>
网络编程
查看>>
关于“设计模式”和“设计程序语言”的一些闲话
查看>>