379. Design Phone Directory

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);

Solution1:Queue + Hashset

思路: queue for getting, and hashmap for checking
Time Complexity: O(1) Space Complexity: O(N)

Solution2:只用一個(gè)Hashset?個(gè)人認(rèn)為不行

思路:如果只用set的話即寡,get用:set的遍歷
Controversy:有可能set遍歷验庙,即使是get1個(gè)的話也可能是O(N)的 想象遍歷bucket一樣

Hashset: This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets. Iterating over this set requires time proportional to the sum of the HashSet instance's size (the number of elements) plus the "capacity" of the backing HashMap instance (the number of buckets). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important

HashSet is imlemented using a HashMap where the elements are the map keys. Since a map has a defined number of buckets that can contain one or more elements, iteration needs to check each bucket, whether it contains elements or not.

Time Complexity: O(N) Space Complexity: O(N)

Solution Code:

class PhoneDirectory {

    /** Initialize your data structure here
        @param maxNumbers - The maximum numbers that can be stored in the phone directory. */
    Set<Integer> used;
    Queue<Integer> available;
    
    public PhoneDirectory(int maxNumbers) {
        used = new HashSet<Integer>();
        available = new LinkedList<Integer>();
        
        for(int i = 0; i < maxNumbers; i++) {
            available.offer(i);
        }    
    }
    
    /** Provide a number which is not assigned to anyone.
        @return - Return an available number. Return -1 if none is available. */
    public int get() {
        Integer ret = available.poll();
        if(ret == null) return -1;
        used.add(ret);
        return ret;
    }
    
    /** Check if a number is available or not. */
    public boolean check(int number) {
        return !used.contains(number);
    }
    
    /** Recycle or release a number. */
    public void release(int number) {
        if(used.remove(number)) {
            available.offer(number);
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市父丰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌掘宪,老刑警劉巖蛾扇,帶你破解...
    沈念sama閱讀 223,126評(píng)論 6 520
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異魏滚,居然都是意外死亡镀首,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,421評(píng)論 3 400
  • 文/潘曉璐 我一進(jìn)店門鼠次,熙熙樓的掌柜王于貴愁眉苦臉地迎上來更哄,“玉大人,你說我怎么就攤上這事腥寇〕婶妫” “怎么了?”我有些...
    開封第一講書人閱讀 169,941評(píng)論 0 366
  • 文/不壞的土叔 我叫張陵赦役,是天一觀的道長(zhǎng)麻敌。 經(jīng)常有香客問我,道長(zhǎng)掂摔,這世上最難降的妖魔是什么术羔? 我笑而不...
    開封第一講書人閱讀 60,294評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮乙漓,結(jié)果婚禮上级历,老公的妹妹穿的比我還像新娘。我一直安慰自己叭披,他們只是感情好寥殖,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,295評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著趋观,像睡著了一般扛禽。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上皱坛,一...
    開封第一講書人閱讀 52,874評(píng)論 1 314
  • 那天编曼,我揣著相機(jī)與錄音,去河邊找鬼剩辟。 笑死掐场,一個(gè)胖子當(dāng)著我的面吹牛往扔,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播熊户,決...
    沈念sama閱讀 41,285評(píng)論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼萍膛,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了嚷堡?” 一聲冷哼從身側(cè)響起蝗罗,我...
    開封第一講書人閱讀 40,249評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蝌戒,沒想到半個(gè)月后串塑,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,760評(píng)論 1 321
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡北苟,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,840評(píng)論 3 343
  • 正文 我和宋清朗相戀三年桩匪,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片友鼻。...
    茶點(diǎn)故事閱讀 40,973評(píng)論 1 354
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡傻昙,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出彩扔,到底是詐尸還是另有隱情妆档,我是刑警寧澤,帶...
    沈念sama閱讀 36,631評(píng)論 5 351
  • 正文 年R本政府宣布虫碉,位于F島的核電站过吻,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏蔗衡。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,315評(píng)論 3 336
  • 文/蒙蒙 一乳绕、第九天 我趴在偏房一處隱蔽的房頂上張望绞惦。 院中可真熱鬧,春花似錦洋措、人聲如沸济蝉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,797評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽王滤。三九已至,卻和暖如春滓鸠,著一層夾襖步出監(jiān)牢的瞬間雁乡,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,926評(píng)論 1 275
  • 我被黑心中介騙來泰國(guó)打工糜俗, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留踱稍,地道東北人曲饱。 一個(gè)月前我還...
    沈念sama閱讀 49,431評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像珠月,于是被迫代替她去往敵國(guó)和親扩淀。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,982評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容