国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

Design Phone Directory

NicolasHe / 2376人閱讀

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

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

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

轉載請注明本文地址:http://specialneedsforspecialkids.com/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實戰(2)整合dva開發后臺管理系統

    摘要:官方推薦結合使用更配哦,其實他們都是同一位開發者開發的,屬于阿里內部開源框架。但是名字必須為,不然不能自動注冊。只有一個的話,可以不用建目錄。可能是我理解有誤。 umi官方推薦結合dva使用更配哦,其實他們都是同一位開發者開發的,屬于阿里內部開源框架。 1 修改.umirc.js,開啟dva支持 // ref: https://umijs.org/config/ export def...

    beita 評論0 收藏0

發表評論

0條評論

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