Leetcode: Design Phone Directory

Posted neverlandly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode: Design Phone Directory相关的知识,希望对你有一定的参考价值。

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

 1 public class PhoneDirectory {
 2     ArrayList<Integer> arr;
 3     HashSet<Integer> set;
 4 
 5     /** Initialize your data structure here
 6         @param maxNumbers - The maximum numbers that can be stored in the phone directory. */
 7     public PhoneDirectory(int maxNumbers) {
 8         arr = new ArrayList<Integer>();
 9         set = new HashSet<Integer>();
10         for (int i=0; i<maxNumbers; i++) {
11             arr.add(i);
12             set.add(i);
13         }
14     }
15     
16     /** Provide a number which is not assigned to anyone.
17         @return - Return an available number. Return -1 if none is available. */
18     public int get() {
19         if (arr.size() == 0) return -1;
20         Random random = new Random();
21         int index = random.nextInt(arr.size());
22         int temp = arr.get(index);
23         arr.set(index, arr.get(arr.size()-1));
24         arr.set(arr.size()-1, temp);
25         arr.remove(arr.size()-1);
26         set.remove(temp);
27         return temp;
28     }
29     
30     /** Check if a number is available or not. */
31     public boolean check(int number) {
32         return set.contains(number);
33     }
34     
35     /** Recycle or release a number. */
36     public void release(int number) {
37         if (!set.contains(number)) {
38             arr.add(number);
39             set.add(number);
40         }
41     }
42 }
43 
44 /**
45  * Your PhoneDirectory object will be instantiated and called as such:
46  * PhoneDirectory obj = new PhoneDirectory(maxNumbers);
47  * int param_1 = obj.get();
48  * boolean param_2 = obj.check(number);
49  * obj.release(number);
50  */

HashSet+ Queue网上vote最高的solution, 

 1 Set<Integer> used = new HashSet<Integer>();
 2     Queue<Integer> available = new LinkedList<Integer>();
 3     int max;
 4     public PhoneDirectory(int maxNumbers) {
 5         max = maxNumbers;
 6         for (int i = 0; i < maxNumbers; i++) {
 7             available.offer(i);
 8         }
 9     }
10     
11     public int get() {
12         Integer ret = available.poll();
13         if (ret == null) {
14             return -1;
15         }
16         used.add(ret);
17         return ret;
18     }
19     
20     public boolean check(int number) {
21         if (number >= max || number < 0) {
22             return false;
23         }
24         return !used.contains(number);
25     }
26     
27     public void release(int number) {
28         if (used.remove(number)) {
29             available.offer(number);
30         }
31     }

 

以上是关于Leetcode: Design Phone Directory的主要内容,如果未能解决你的问题,请参考以下文章

379. Design Phone Directory

design studio是啥意思

leetcode 355 Design Twitte

LeetCode 706. Design HashMap

LeetCode Design Hit Counter

LeetCode 622. Design Circular Queue