成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] Design Phone Directory

wangbinke / 2754人閱讀

Problem

Design a Phone Directory which supports the following operations:

get: Provide a number which is not assigned to anyone.
check: Check if a number is available or not.
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);

Solution
    Set used = new HashSet();
    Queue available = new LinkedList();
    int max;
    public PhoneDirectory(int maxNumbers) {
        max = maxNumbers;
        for (int i = 0; i < maxNumbers; i++) {
            available.offer(i);
        }
    }
    
    public int get() {
        Integer ret = available.poll(); //Use Integer in case ret is null
        if (ret == null) {
            return -1; //Handle null exception
        }
        used.add(ret);
        return ret;
    }
    
    public boolean check(int number) { //Use HashSet only
        if (number >= max || number < 0) {
            return false;
        }
        return !used.contains(number); 
    }
    
    public void release(int number) { //from used to available
        if (used.remove(number)) {
            available.offer(number);
        }
    }

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/66133.html

相關文章

  • Design Phone Directory

    摘要:題目鏈接直接用一個,結(jié)果了看了加了個,不過感覺沒什么必要加,反正保存的都一樣,只是的時間大于,用可以保證??戳祟}目條件是可以隨便返回一個值,但是不讓這么做。很無語啊如果這道題要求要求的是的,那就和一樣了。 Design Phone Directory 題目鏈接:https://leetcode.com/problems... 直接用一個set,結(jié)果tle了= = public clas...

    NicolasHe 評論0 收藏0
  • [LeetCode] 588. Design In-Memory File System

    Problem Design an in-memory file system to simulate the following functions: ls: Given a path in string format. If it is a file path, return a list that only contains this files name. If it is a direc...

    SHERlocked93 評論0 收藏0
  • [Leetcode] Letter Combinations of a Phone Number 電

    摘要:最新更新請見深度優(yōu)先搜索復雜度時間空間遞歸??臻g思路首先建一個表,來映射號碼和字母的關系。然后對號碼進行深度優(yōu)先搜索,對于每一位,從表中找出數(shù)字對應的字母,這些字母就是本輪搜索的幾種可能。 Letter Combinations of a Phone Number 最新更新請見:https://yanjia.me/zh/2019/01/... Given a digit string...

    fxp 評論0 收藏0
  • leetcode17 Letter Combinations of a Phone Number

    摘要:題目要求也就是說,將數(shù)字對應的字母的排列組合的的所有可能結(jié)果都枚舉出來,順序不唯一。這種類型的題目一般需要求出上一種情況的前提下才可以得知下一種情況。這一種數(shù)據(jù)結(jié)構(gòu)通過來實現(xiàn)。相比于上一種思路中,內(nèi)存占用更小,而且更加靈活。 題目要求 Given a digit string, return all possible letter combinations that the numbe...

    snowell 評論0 收藏0
  • leetcode 17 Letter Combinations of a Phone Number

    摘要:而按鍵和字母的對應關系如上圖。這將成為下一次操作的前序字符串。對于每一個不同的前序字符串,我們都要在其后面分別加上當前鍵所表示的不同字符,再將獲得的結(jié)果字符串加入里面。 題目詳情 Given a digit string, return all possible letter combinations that the number could represent. mapping o...

    sean 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<