379. Design Phone Directory

 

Design a Phone Directory which supports the following operations:

  1. get: Provide a number which is not assigned to anyone.
  2. check: Check if a number is available or not.
  3. release: Recycle or release a number.

Example:

// Init a phone directory containing a total of 3 numbers: 0, 1, and 2.
PhoneDirectory directory = new PhoneDirectory(3);

// It can return any available phone number. Here we assume it returns 0.
directory.get();

// Assume it returns 1.
directory.get();

// The number 2 is available, so return true.
directory.check(2);

// It returns 2, the only number that is left.
directory.get();

// The number 2 is no longer available, so return false.
directory.check(2);

// Release number 2 back to the pool.
directory.release(2);

// Number 2 is available again, return true.
directory.check(2);

 

又是一道设计题,让我们设计一个电话目录管理系统,可以分配电话号码,查询某一个号码是否已经被使用,释放一个号码。既然要分配号码,肯定需要一个数组 nums 来存所有可以分配的号码,注意要初始化成不同的数字。然后再用一个长度相等的数组 used 来标记某个位置上的号码是否已经被使用过了,用一个变量 idx 表明当前分配到的位置。再 get 函数中,首先判断若 idx 小于0了,说明没有号码可以分配了,返回 -1。否则就取出 nums[idx],并且标记该号码已经使用了,注意 idx 还要自减1,返回之前取出的号码。对于 check 函数,直接在 used 函数中看对应值是否为0。最后实现 release 函数,若该号码没被使用过,直接 return;否则将 idx 自增1,再将该号码赋值给 nums[idx],然后在 used 中标记为0即可,参见代码如下:

 

解法一:

class PhoneDirectory {
public:
    PhoneDirectory(int maxNumbers) {
        nums.resize(maxNumbers);
        used.resize(maxNumbers);
        idx = maxNumbers - 1;
        iota(nums.begin(), nums.end(), 0);
    }
    int get() {
        if (idx < 0) return -1;
        int num = nums[idx--];
        used[num] = 1;
        return num;
    }
    bool check(int number) {
        return used[number] == 0;
    }
    void release(int number) {
        if (used[number] == 0) return;
        nums[++idx] = number;
        used[number] = 0;
    }

private:
    int idx;
    vector<int> nums, used;
};

 

我们也可以使用队列 queue 和 HashSet 来做,整个思想和上面没有啥太大的区别,就是写法上略有不同,参见代码如下:

 

解法二:

class PhoneDirectory {
public:
    PhoneDirectory(int maxNumbers) {
        mx = maxNumbers;
        for (int i = 0; i < maxNumbers; ++i) q.push(i);
    }
    int get() {
        if (q.empty()) return -1;
        int num = q.front(); q.pop();
        used.insert(num);
        return num;
    }
    bool check(int number) {
        return !used.count(number);
    }
    void release(int number) {
        if (!used.count(number)) return;
        used.erase(number);
        q.push(number);
    }
    
private:
    int mx;
    queue<int> q;
    unordered_set<int> used;
};

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/379

 

参考资料:

https://leetcode.com/problems/design-phone-directory/

https://leetcode.com/problems/design-phone-directory/discuss/85328/Java-AC-solution-using-queue-and-set

https://leetcode.com/problems/design-phone-directory/discuss/122908/Java-O(1)-time-o(n)-space-single-Array-99ms-beats-100

 

LeetCode All in One 题目讲解汇总(持续更新中…)


转载请注明来源于 Grandyang 的博客 (grandyang.com),欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 grandyang@qq.com

💰


微信打赏


Venmo 打赏

×

Help us with donation