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

資訊專欄INFORMATION COLUMN

Design Phone Directory

NicolasHe / 2380人閱讀

摘要:題目鏈接直接用一個,結果了看了加了個,不過感覺沒什么必要加,反正保存的都一樣,只是的時間大于,用可以保證??戳祟}目條件是可以隨便返回一個值,但是不讓這么做。很無語啊如果這道題要求要求的是的,那就和一樣了。

Design Phone Directory

題目鏈接:https://leetcode.com/problems...

直接用一個set,結果tle了= =

public class PhoneDirectory {

    /** Initialize your data structure here
        @param maxNumbers - The maximum numbers that can be stored in the phone directory. */
    Set set = new HashSet();
    int maxNum;
    public PhoneDirectory(int maxNumbers) {
        for(int i = 0; i < maxNumbers; i++) set.add(i);
        maxNum = maxNumbers;
    }
    
    /** Provide a number which is not assigned to anyone.
        @return - Return an available number. Return -1 if none is available. */
    public int get() {
        // no available numbers left
        if(set.size() == 0) return -1;
        // return 1 number
        else {
            int number = set.iterator().next();
            set.remove(number);
            return number;
        }
    }
    
    /** Check if a number is available or not. */
    public boolean check(int number) {
        return set.contains(number);
    }
    
    /** Recycle or release a number. */
    public void release(int number) {
        if(number >= 0 && number < maxNum) set.add(number);
    }
}

看了discussion加了個queue,不過感覺沒什么必要加q,反正保存的都一樣,只是set.iterator().next()的時間大于O(1),用q可以保證O(1)??戳祟}目條件是get可以隨便返回一個值,但是test case不讓這么做。很無語啊

public class PhoneDirectory {

    /** Initialize your data structure here
        @param maxNumbers - The maximum numbers that can be stored in the phone directory. */
    Set set = new HashSet();
    int maxNum;
    Queue available = new LinkedList();
    public PhoneDirectory(int maxNumbers) {
        for(int i = 0; i < maxNumbers; i++) available.add(i);
        maxNum = maxNumbers;
    }
    
    /** Provide a number which is not assigned to anyone.
        @return - Return an available number. Return -1 if none is available. */
    public int get() {
        // no available numbers left
        if(available.size() == 0) return -1;
        // return 1 number
        else {
            int number = available.poll();
            set.add(number);
            return number;
        }
    }
    
    /** Check if a number is available or not. */
    public boolean check(int number) {
        return !set.contains(number);
    }
    
    /** Recycle or release a number. */
    public void release(int number) {
        if(number >= 0 && number < maxNum) {
            if(set.remove(number)) available.add(number);
        }
    }
}

如果這道題要求get要求的是random的,那就和380. Insert Delete GetRandom O(1)一樣了。
https://segmentfault.com/a/11...

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

轉載請注明本文地址:http://systransis.cn/yun/66633.html

相關文章

  • [LeetCode] Design Phone Directory

    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...

    wangbinke 評論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
  • React框架Umi實戰(zhàn)(2)整合dva開發(fā)后臺管理系統(tǒng)

    摘要:官方推薦結合使用更配哦,其實他們都是同一位開發(fā)者開發(fā)的,屬于阿里內(nèi)部開源框架。但是名字必須為,不然不能自動注冊。只有一個的話,可以不用建目錄??赡苁俏依斫庥姓`。 umi官方推薦結合dva使用更配哦,其實他們都是同一位開發(fā)者開發(fā)的,屬于阿里內(nèi)部開源框架。 1 修改.umirc.js,開啟dva支持 // ref: https://umijs.org/config/ export def...

    beita 評論0 收藏0

發(fā)表評論

0條評論

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