• 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]]
             */
        }
    }
    
  • 相关阅读:
    Elasticsearch Query DSL 整理总结(三)—— Match Phrase Query 和 Match Phrase Prefix Query
    Elasticsearch Query DSL 整理总结(二)—— 要搞懂 Match Query,看这篇就够了
    Elasticsearch Query DSL 整理总结(一)—— Query DSL 概要,MatchAllQuery,全文查询简述
    Elasticsearch Java Rest Client API 整理总结 (三)——Building Queries
    Elasticsearch date 类型详解
    python 历险记(五)— python 中的模块
    python 历险记(四)— python 中常用的 json 操作
    python 历险记(三)— python 的常用文件操作
    Elasticsearch Java Rest Client API 整理总结 (二) —— SearchAPI
    Elasticsearch Java Rest Client API 整理总结 (一)——Document API
  • 原文地址:https://www.cnblogs.com/optor/p/8588969.html
Copyright © 2020-2023  润新知