摘要:题目链接直接用一个,结果了看了加了个,不过感觉没什么必要加,反正保存的都一样,只是的时间大于,用可以保证。看了题目条件是可以随便返回一个值,但是不让这么做。很无语啊如果这道题要求要求的是的,那就和一样了。
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...
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/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...
阅读 4886·2023-04-25 18:47
阅读 2657·2021-11-19 11:33
阅读 3424·2021-11-11 16:54
阅读 3091·2021-10-26 09:50
阅读 2529·2021-10-14 09:43
阅读 648·2021-09-03 10:47
阅读 657·2019-08-30 15:54
阅读 1472·2019-08-30 15:44