摘要:題目鏈接直接用一個,結果了看了加了個,不過感覺沒什么必要加,反正保存的都一樣,只是的時間大于,用可以保證。看了題目條件是可以隨便返回一個值,但是不讓這么做。很無語啊如果這道題要求要求的是的,那就和一樣了。
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. */ Setset = 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. */ Setset = 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
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...
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...
摘要:官方推薦結合使用更配哦,其實他們都是同一位開發者開發的,屬于阿里內部開源框架。但是名字必須為,不然不能自動注冊。只有一個的話,可以不用建目錄。可能是我理解有誤。 umi官方推薦結合dva使用更配哦,其實他們都是同一位開發者開發的,屬于阿里內部開源框架。 1 修改.umirc.js,開啟dva支持 // ref: https://umijs.org/config/ export def...
閱讀 4928·2023-04-25 18:47
閱讀 2680·2021-11-19 11:33
閱讀 3450·2021-11-11 16:54
閱讀 3105·2021-10-26 09:50
閱讀 2546·2021-10-14 09:43
閱讀 672·2021-09-03 10:47
閱讀 675·2019-08-30 15:54
閱讀 1504·2019-08-30 15:44