729. My Calendar I (java)

Implement a MyCalendar class to store your events. A new event can be added if adding the event will not cause a double booking.

Your class will have the method, book(int start, int end). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start <= x < end.

A double booking happens when two events have some non-empty intersection (ie., there is some time that is common to both events.)

For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.

Your class will be called like this: MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)

Example 1:

MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(15, 25); // returns false
MyCalendar.book(20, 30); // returns true
Explanation: 
The first event can be booked.  The second can't because time 15 is already booked by another event.
The third event can be booked, as the first event takes every time less than 20, but not including 20.

Note:

The number of calls to MyCalendar.book per test case will be at most 1000.
In calls to MyCalendar.book(start, end), start and end are integers in the range [0, 10^9].

思路:logn的时间复杂度,用treemap来做,也可以遍历一遍,但时间复杂度是O(n)
class MyCalendar {
    public TreeMap<Integer, Integer> s;
    public TreeMap<Integer, Integer> e;
    
    public MyCalendar() {
        s = new TreeMap<>();
        e = new TreeMap<>();
    }
    
    public boolean book(int start, int end) {
        if (s.size() == 0) {
            s.put(start, end);
            e.put(end, start);
            return true;
        }
        if (s.lowerKey(end)!= null && s.get(s.lowerKey(end)) > start) 
            return false;
        if (e.higherKey(start) != null && e.get(e.higherKey(start)) < end) 
            return false;
        s.put(start, end);
        e.put(end, start);
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.GregorianCalendar; class MyCalendar extends JFrame implements ActionListener { private JPanel p1, p2, p3; //三个面板 p1最上面 p2中间 p3下面 private JLabel yearStr, monthStr; //标签 private JTextField inputYear, inputMonth; private JButton confirm; //确认 private JButton lastMonth; private JButton nextMonth; private JLabel dayText;//文本框 private JLabel TimeText;//文本框 //p2面板里控件的声明 private String[] week = {"日", "一", "二", "三", "四", "五", "六"}; private JLabel[] weekLable = new JLabel[week.length];//数组的声明 //p3面板的42个按钮声明 private JButton[] dayBtn = new JButton[42]; private Calendar nowDate = new GregorianCalendar(); //Calendar是抽象类 new不出 用直接子类 private int nowYear = nowDate.get(Calendar.YEAR); private int nowMonth = nowDate.get(Calendar.MONTH); public MyCalendar() throws HeadlessException { p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); yearStr = new JLabel("Year:"); inputYear = new JTextField(4); monthStr = new JLabel(" Month:"); inputMonth = new JTextField(3); confirm = new JButton("确认"); lastMonth = new JButton("上月"); nextMonth = new JButton("下月"); dayText = new JLabel(); TimeText = new JLabel(); new Thread() { //线程内部类用来实时显示时间 public void run() { while (true) { LocalDateTime dateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); //大的HH是24小时制的 String nowTime = dateTime.format(formatter); TimeText.setText(nowTime); try { Thread.sleep(1000); } catch (Inter
最新发布
06-02
以下是使用 Java Swing 创建一个简单的数字时钟,它使用 `Calendar` 类中的 `hour`、`minute` 和 `second` 方法来获取当前时间,并将其显示在用户界面上: ```java import java.awt.Font; import java.awt.GridLayout; import java.util.Calendar; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class DigitalClock extends JFrame implements Runnable { private JLabel hourLabel; private JLabel minuteLabel; private JLabel secondLabel; public DigitalClock() { super("Digital Clock"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(200, 80); JPanel panel = new JPanel(new GridLayout(1, 3)); Font font = new Font("Verdana", Font.BOLD, 24); hourLabel = new JLabel("00"); hourLabel.setFont(font); panel.add(hourLabel); minuteLabel = new JLabel("00"); minuteLabel.setFont(font); panel.add(minuteLabel); secondLabel = new JLabel("00"); secondLabel.setFont(font); panel.add(secondLabel); add(panel); setVisible(true); } @Override public void run() { while (true) { Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); hourLabel.setText(String.format("%02d", hour)); minuteLabel.setText(String.format("%02d", minute)); secondLabel.setText(String.format("%02d", second)); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { SwingUtilities.invokeLater(new DigitalClock()); } } ``` 在这个例子中,我们创建了一个 `DigitalClock` 类,它继承了 `JFrame` 并实现了 `Runnable` 接口。我们在 `DigitalClock` 构造函数中创建了一个包含 3 个标签的面板,并将它们添加到窗口中。我们使用 `Calendar` 类来获取当前时间,并将其格式化为两位数的字符串,然后将其设置为标签的文本。最后,我们在 `run` 方法中使用 `Thread.sleep` 方法来使线程等待一秒钟,然后再次更新标签的文本。我们在 `main` 方法中使用 `SwingUtilities.invokeLater` 方法来启动我们的应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值