Design Phone Directory

 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

Design Phone Directory的相关文章

379. 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

[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

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

【LeetCode】设计题 design(共38题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure design [173]Binary Search Tree Iterator [208]Implement Trie (Prefix Tree) [211]Add and Search Word - Data structure desig

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

leetcode 链表题总结

按照frequency来排序,总共27题 1.2. Add Two Numbers https://leetcode.com/problems/add-two-numbers/#/description 两个链表相加,注意就是进位问题. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } *

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

斯坦福CS课程列表

http://exploredegrees.stanford.edu/coursedescriptions/cs/ CS 101. Introduction to Computing Principles. 3-5 Units. Introduces the essential ideas of computing: data representation, algorithms, programming "code", computer hardware, networking, s

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea