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

my HashSet+ ArrayList, 删除的时候把要删的index与末尾对调。get()其实不需要random, 因为anyone is ok

 public class PhoneDirectory {
ArrayList<Integer> arr;
HashSet<Integer> set; /** Initialize your data structure here
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
public PhoneDirectory(int maxNumbers) {
arr = new ArrayList<Integer>();
set = new HashSet<Integer>();
for (int i=0; i<maxNumbers; i++) {
arr.add(i);
set.add(i);
}
} /** Provide a number which is not assigned to anyone.
@return - Return an available number. Return -1 if none is available. */
public int get() {
if (arr.size() == 0) return -1;
Random random = new Random();
int index = random.nextInt(arr.size());
int temp = arr.get(index);
arr.set(index, arr.get(arr.size()-1));
arr.set(arr.size()-1, temp);
arr.remove(arr.size()-1);
set.remove(temp);
return temp;
} /** 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 (!set.contains(number)) {
arr.add(number);
set.add(number);
}
}
} /**
* Your PhoneDirectory object will be instantiated and called as such:
* PhoneDirectory obj = new PhoneDirectory(maxNumbers);
* int param_1 = obj.get();
* boolean param_2 = obj.check(number);
* obj.release(number);
*/

HashSet+ Queue网上vote最高的solution,

 public class PhoneDirectory {

     Set<Integer> used = new HashSet<Integer>();
Queue<Integer> available = new LinkedList<Integer>();
int max;
public PhoneDirectory(int maxNumbers) {
max = maxNumbers;
for (int i = 0; i < maxNumbers; i++) {
available.offer(i);
}
} public int get() {
Integer ret = available.poll();
if (ret == null) {
return -1;
}
used.add(ret);
return ret;
} public boolean check(int number) {
if (number >= max || number < 0) {
return false;
}
return !used.contains(number);
} public void release(int number) {
if (used.remove(number)) {
available.offer(number);
}
}
} /**
* Your PhoneDirectory object will be instantiated and called as such:
* PhoneDirectory obj = new PhoneDirectory(maxNumbers);
* int param_1 = obj.get();
* boolean param_2 = obj.check(number);
* obj.release(number);
*/

Leetcode: Design Phone Directory的更多相关文章

  1. [LeetCode] Design Phone Directory 设计电话目录

    Design a Phone Directory which supports the following operations: get: Provide a number which is not ...

  2. 379. Design Phone Directory

    379. Design Phone Directory Design a Phone Directory which supports the following operations: get: P ...

  3. [LeetCode] 379. Design Phone Directory 设计电话目录

    Design a Phone Directory which supports the following operations: get: Provide a number which is not ...

  4. 【LeetCode】379. Design Phone Directory 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组 日期 题目地址:https://leetcode ...

  5. [LeetCode] Design In-Memory File System 设计内存文件系统

    Design an in-memory file system to simulate the following functions: ls: Given a path in string form ...

  6. [LeetCode] Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  7. [LeetCode] Design Twitter 设计推特

    Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...

  8. [LeetCode] Design Snake Game 设计贪吃蛇游戏

    Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...

  9. [LeetCode] Design Tic-Tac-Toe 设计井字棋游戏

    Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...

随机推荐

  1. JS冒泡排序(数组)

    冒泡排序是把数组相邻的两个值进行比较,然后根据条件执行相应的命令 var arr = [0,4,8,5,2,7,1,3,6,9]; for(var s = 0;s<arr.length;s++) ...

  2. js判断是iOS还是Android

    <script type="text/javascript"> var ua = navigator.userAgent.toLowerCase(); if (/iph ...

  3. 【贪心】POJ 1065

    头一次接触POJ,然后写了自己比较擅长的贪心. 解题思路大概就是从小排(这个很重要,然后用cmp随便长度或者重量的排序,选择最小的开始) 直到所有比他weight大的,没有符合条件的了.就代表要再加一 ...

  4. Redis 数据类型总结—String

    1.1 数据类型 Redis常用五种数据类型:string,   hash,   list,   set,    zset(sorted set). Redis内部使用一个redisObject对象来 ...

  5. discuz sphinx全文检索搜索引擎方案

    基于discuz的索引配置文件,这个配置文件比较灵活,可以根据不同的需求来配置 # # linuxTone full index search configure file # source lt_p ...

  6. Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  7. css实现 当鼠标移到input的时候,input框出现阴影,当移动到button的时候,input框的阴影消失,button框出现阴影

    <meta charset="utf-8" /> <style type="text/css"> div{overflow: hidde ...

  8. 复制文件的问题:使用FileInputStream和FileOutputStream实现文件复制

    public class Test{ public static void main(String [] args) { Test t=new Test(); t.upload(); } public ...

  9. PHP关于反斜杠处理函数addslashes()和stripslashes()的用法

    addslashes() 例子: <?php $str = "Who's John Adams?"; echo $str . " This is not safe ...

  10. UI控件闪烁及刷新

    我们常常在一个窗口上放置很多控件,在改变窗口大小时,控件会跟着一起闪烁... 此时,可以将窗口添加WS_CLIPCHILDREN属性即可.(如果包含多层,都需要WS_CLIPCHILDREN属性) 默 ...