Skip to main content

🟡 剑指 Offer II 058. 日程表

LeetCode 提示

题目难度 中等

原题链接 🔗 leetcode

题解1#

Java treemap & treeset 真是好用啊

treemap.floorKey treemap.ceilingKey treeset.floor treeset.ceiling

class MyCalendar {
    private TreeMap<Integer, Integer> cal;
    public MyCalendar() {        this.cal = new TreeMap<>();    }        public boolean book(int start, int end) {        Integer prev = cal.floorKey(start);        Integer next = cal.ceilingKey(start);
        if ((prev == null || cal.get(prev) <= start) && (next == null || next >= end)) {            cal.put(start, end);            return true;        }        return false;    }}
/** * Your MyCalendar object will be instantiated and called as such: * MyCalendar obj = new MyCalendar(); * boolean param_1 = obj.book(start,end); */