zoukankan      html  css  js  c++  java
  • 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]]
             */
        }
    }
    
  • 相关阅读:
    数据库镜像搭建
    关于开发人员数据库权限配置以及规范数据库升级流程
    带CheckBox列头的DataGridView
    查询整个数据库中某个特定值所在的表和字段的方法
    SQL Server 2008中获取数据库所有表及其字段名称、类型、长度的SQL
    关于已经上线项目的升级的启示
    SQL语句恢复数据库时一直显示“正在还原”
    带CheckBox列头的DataGridView(一)
    SQL Server中事务处理的注意事项
    group by 使用
  • 原文地址:https://www.cnblogs.com/optor/p/8588969.html
Copyright © 2011-2022 走看看