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]]
         */
    }
}
原文地址:https://www.cnblogs.com/optor/p/8588969.html