返回介绍

solution / 0300-0399 / 0379.Design Phone Directory / README_EN

发布于 2024-06-17 01:04:01 字数 4721 浏览 0 评论 0 收藏 0

379. Design Phone Directory

中文文档

Description

Design a phone directory that initially has maxNumbers empty slots that can store numbers. The directory should store numbers, check if a certain slot is empty or not, and empty a given slot.

Implement the PhoneDirectory class:

  • PhoneDirectory(int maxNumbers) Initializes the phone directory with the number of available slots maxNumbers.
  • int get() Provides a number that is not assigned to anyone. Returns -1 if no number is available.
  • bool check(int number) Returns true if the slot number is available and false otherwise.
  • void release(int number) Recycles or releases the slot number.

 

Example 1:

Input
["PhoneDirectory", "get", "get", "check", "get", "check", "release", "check"]
[[3], [], [], [2], [], [2], [2], [2]]
Output
[null, 0, 1, true, 2, false, null, true]

Explanation
PhoneDirectory phoneDirectory = new PhoneDirectory(3);
phoneDirectory.get();    // It can return any available phone number. Here we assume it returns 0.
phoneDirectory.get();    // Assume it returns 1.
phoneDirectory.check(2);   // The number 2 is available, so return true.
phoneDirectory.get();    // It returns 2, the only number that is left.
phoneDirectory.check(2);   // The number 2 is no longer available, so return false.
phoneDirectory.release(2); // Release number 2 back to the pool.
phoneDirectory.check(2);   // Number 2 is available again, return true.

 

Constraints:

  • 1 <= maxNumbers <= 104
  • 0 <= number < maxNumbers
  • At most 2 * 104 calls will be made to get, check, and release.

Solutions

Solution 1

class PhoneDirectory:
  def __init__(self, maxNumbers: int):
    """
    Initialize your data structure here
    @param maxNumbers - The maximum numbers that can be stored in the phone directory.
    """
    self.provided = [False] * maxNumbers

  def get(self) -> int:
    """
    Provide a number which is not assigned to anyone.
    @return - Return an available number. Return -1 if none is available.
    """
    for i in range(len(self.provided)):
      if not self.provided[i]:
        self.provided[i] = True
        return i
    return -1

  def check(self, number: int) -> bool:
    """
    Check if a number is available or not.
    """
    return not self.provided[number]

  def release(self, number: int) -> None:
    """
    Recycle or release a number.
    """
    self.provided[number] = False


# Your PhoneDirectory object will be instantiated and called as such:
# obj = PhoneDirectory(maxNumbers)
# param_1 = obj.get()
# param_2 = obj.check(number)
# obj.release(number)
class PhoneDirectory {

  private boolean[] provided;

  /**
     Initialize your data structure here
    @param maxNumbers - The maximum numbers that can be stored in the phone directory.
   */
  public PhoneDirectory(int maxNumbers) {
    provided = new boolean[maxNumbers];
  }

  /**
     Provide a number which is not assigned to anyone.
    @return - Return an available number. Return -1 if none is available.
   */
  public int get() {
    for (int i = 0; i < provided.length; ++i) {
      if (!provided[i]) {
        provided[i] = true;
        return i;
      }
    }
    return -1;
  }

  /** Check if a number is available or not. */
  public boolean check(int number) {
    return !provided[number];
  }

  /** Recycle or release a number. */
  public void release(int number) {
    provided[number] = false;
  }
}

/**
 * Your PhoneDirectory object will be instantiated and called as such:
 * PhoneDirectory obj = new PhoneDirectory(maxNumbers);
 * int param_1 = obj.get();
 * boolean param_2 = obj.check(number);
 * obj.release(number);
 */

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文