1 public class PhoneDirectory { 2 private boolean[] nums; 3 private Queue<Integer> queue; 4 /** Initialize your data structure here 5 @param maxNumbers - The maximum numbers that can be stored in the phone directory. */ 6 public PhoneDirectory(int maxNumbers) { 7 nums = new boolean[maxNumbers]; 8 queue = new LinkedList<>(); 9 for (int i = 0; i < maxNumbers; i++) { 10 queue.offer(i); 11 } 12 } 13 14 /** Provide a number which is not assigned to anyone. 15 @return - Return an available number. Return -1 if none is available. */ 16 public int get() { 17 if (queue.isEmpty()) { 18 return -1; 19 } 20 int result = queue.poll(); 21 nums[result] = true; 22 return result; 23 } 24 25 /** Check if a number is available or not. */ 26 public boolean check(int number) { 27 return !nums[number]; 28 } 29 30 /** Recycle or release a number. */ 31 public void release(int number) { 32 if (!nums[number]) { 33 return; 34 } 35 nums[number] = false; 36 queue.offer(number); 37 } 38 } 39 40 /** 41 * Your PhoneDirectory object will be instantiated and called as such: 42 * PhoneDirectory obj = new PhoneDirectory(maxNumbers); 43 * int param_1 = obj.get(); 44 * boolean param_2 = obj.check(number); 45 * obj.release(number); 46 */
时间: 2024-10-27 10:19:14