zoukankan      html  css  js  c++  java
  • Java实现 LeetCode 729 我的日程安排表 I(二叉树)

    729. 我的日程安排表 I

    实现一个 MyCalendar 类来存放你的日程安排。如果要添加的时间内没有其他安排,则可以存储这个新的日程安排。

    MyCalendar 有一个 book(int start, int end)方法。它意味着在 start 到 end 时间内增加一个日程安排,注意,这里的时间是半开区间,即 [start, end), 实数 x 的范围为, start <= x < end。

    当两个日程安排有一些时间上的交叉时(例如两个日程安排都在同一时间内),就会产生重复预订。

    每次调用 MyCalendar.book方法时,如果可以将日程安排成功添加到日历中而不会导致重复预订,返回 true。否则,返回 false 并且不要将该日程安排添加到日历中。

    请按照以下步骤调用 MyCalendar 类: MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)

    示例 1:

    MyCalendar();
    MyCalendar.book(10, 20); // returns true
    MyCalendar.book(15, 25); // returns false
    MyCalendar.book(20, 30); // returns true
    解释:
    第一个日程安排可以添加到日历中. 第二个日程安排不能添加到日历中,因为时间 15 已经被第一个日程安排预定了。
    第三个日程安排可以添加到日历中,因为第一个日程安排并不包含时间 20 。
    说明:

    每个测试用例,调用 MyCalendar.book 函数最多不超过 100次。
    调用函数 MyCalendar.book(start, end)时, start 和 end 的取值范围为 [0, 10^9]。

    class TNode{
            int start;
            int end;
            TNode left;
            TNode right;
    
            TNode(int start, int end){
                this.start = start;
                this.end = end;
            }
    
            boolean insert (TNode node){
                if (node.end <= this.start){
                    if (this.left == null){
                        this.left = node;
                        return true;
                    }
                    return this.left.insert(node);
                }
                else if (node.start >= this.end){
                    if (this.right == null){
                        this.right = node;
                        return true;
                    }
                    return this.right.insert(node);
                }
                else{
                    return false;
                }
            }
        }
    
        class MyCalendar {
            TNode root;
            public MyCalendar() {
                root = null;
            }
    
            public boolean book(int start, int end) {
                if (root == null){
                    root = new TNode(start, end);
                    return true;
                }
                return root.insert(new TNode(start, end));
            }
        }
    
    /**
     * Your MyCalendar object will be instantiated and called as such:
     * MyCalendar obj = new MyCalendar();
     * boolean param_1 = obj.book(start,end);
     */
    
  • 相关阅读:
    Spring spEL
    Spring 使用外部部署文件
    Spring 自动装配
    spring 属性配置细节
    hdu 1054 Strategic Game
    fzu 2037 Maximum Value Problem
    将博客搬至CSDN
    HDU 4714 Tree2Cycle
    HDU 1009 The Shortest Path in Nya Graph
    POJ 1942 Paths on a Grid 组合数的优化
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13074745.html
Copyright © 2011-2022 走看看