1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-03-15 00:32:24 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

44 lines
1.8 KiB
HTML
Raw Normal View History

2025-01-09 20:29:41 +08:00
<p>在考场里,有&nbsp;<code>n</code>&nbsp;个座位排成一行,编号为 <code>0</code><code>n - 1</code></p>
2022-03-27 20:46:41 +08:00
2025-01-09 20:29:41 +08:00
<p>当学生进入考场后,他必须坐在离最近的人最远的座位上。如果有多个这样的座位,他会坐在编号最小的座位上。(另外,如果考场里没有人,那么学生就坐在 <code>0</code> 号座位上。)</p>
2022-03-27 20:46:41 +08:00
2025-01-09 20:29:41 +08:00
<p>设计一个模拟所述考场的类。</p>
<p>实现&nbsp;<code>ExamRoom</code>&nbsp;类:</p>
<ul>
<li><code>ExamRoom(int n)</code> 用座位的数量&nbsp;<code>n</code> 初始化考场对象。</li>
<li><code>int seat()</code> 返回下一个学生将会入座的座位编号。</li>
<li><code>void leave(int p)</code> 指定坐在座位 <code>p</code> 的学生将离开教室。保证座位 <code>p</code> 上会有一位学生。</li>
</ul>
2022-03-27 20:46:41 +08:00
<p>&nbsp;</p>
2025-01-09 20:29:41 +08:00
<p><strong>示例 1</strong></p>
2022-03-27 20:46:41 +08:00
2025-01-09 20:29:41 +08:00
<pre>
<strong>输入:</strong>
["ExamRoom", "seat", "seat", "seat", "seat", "leave", "seat"]
[[10], [], [], [], [], [4], []]
<strong>输出:</strong>
[null, 0, 9, 4, 2, null, 5]
2022-03-27 20:46:41 +08:00
<strong>解释:</strong>
2025-01-09 20:29:41 +08:00
ExamRoom examRoom = new ExamRoom(10);
examRoom.seat(); // 返回 0房间里没有人学生坐在 0 号座位。
examRoom.seat(); // 返回 9学生最后坐在 9 号座位。
examRoom.seat(); // 返回 4学生最后坐在 4 号座位。
examRoom.seat(); // 返回 2学生最后坐在 2 号座位。
examRoom.leave(4);
examRoom.seat(); // 返回 5学生最后坐在 5 号座位。
2022-03-27 20:46:41 +08:00
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ol>
2025-01-09 20:29:41 +08:00
<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
<li>保证有学生正坐在座位 <code>p</code> 上。</li>
<li><code>seat</code>&nbsp;&nbsp;<code>leave</code>&nbsp;最多被调用&nbsp;<code>10<sup>4</sup></code>&nbsp;次。</li>
2022-03-27 20:46:41 +08:00
</ol>