码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode-Design Phone Directory

时间:2016-08-08 14:34:39      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

Design a Phone Directory which supports the following operations:

 

  1. get: Provide a number which is not assigned to anyone.
  2. check: Check if a number is available or not.
  3. 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);
 
 Analysis:
 
We record the last used number and also maintain a hashset to record all released number. Everytime get() called, we first get a released number, if not, then use an unused number.
 
Solution:
 1 public class PhoneDirectory {
 2     HashSet<Integer> numReleased;
 3     int maxNum, nextUnuse;
 4 
 5 
 6     /** Initialize your data structure here
 7         @param maxNumbers - The maximum numbers that can be stored in the phone directory. */
 8     public PhoneDirectory(int maxNumbers) {
 9         numReleased = new HashSet<Integer>();
10         maxNum = maxNumbers;
11         nextUnuse = 0;
12         
13     }
14     
15     /** Provide a number which is not assigned to anyone.
16         @return - Return an available number. Return -1 if none is available. */
17     public int get() {
18         int num = -1;
19         if (!numReleased.isEmpty()) {
20             for (Integer i : numReleased) {
21                 num = i;
22                 break;
23             }
24             numReleased.remove(num);
25         } else if (nextUnuse < maxNum) {
26             num = nextUnuse++;
27         }
28         return num;
29     }
30     
31     /** Check if a number is available or not. */
32     public boolean check(int number) {
33         if (number >= maxNum) return false;
34 
35         if (number >= nextUnuse || numReleased.contains(number)) return true;
36 
37         return false;
38     }
39     
40     /** Recycle or release a number. */
41     public void release(int number) {
42         if (number >= nextUnuse)  return;
43         numReleased.add(number);
44     }
45 }
46 
47 /**
48  * Your PhoneDirectory object will be instantiated and called as such:
49  * PhoneDirectory obj = new PhoneDirectory(maxNumbers);
50  * int param_1 = obj.get();
51  * boolean param_2 = obj.check(number);
52  * obj.release(number);
53  */

 

LeetCode-Design Phone Directory

标签:

原文地址:http://www.cnblogs.com/lishiblog/p/5748931.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!