<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)379:電話目錄管理系統(tǒng)

          共 3264字,需瀏覽 7分鐘

           ·

          2021-09-13 15:59

          算法的重要性,我就不多說了吧,想去大廠,就必須要經過基礎知識和業(yè)務邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 電話目錄管理系統(tǒng),我們先來看題面:
          https://leetcode-cn.com/problems/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.



          設計一個電話目錄管理系統(tǒng),讓它支持以下功能:
          get: 分配給用戶一個未被使用的電話號碼,獲取失敗請返回 -1
          check: 檢查指定的電話號碼是否被使用
          release: 釋放掉一個電話號碼,使其能夠重新被分配

          示例


          // 初始化電話目錄,包括 3 個電話號碼:0,1 和 2。
          PhoneDirectory directory = new PhoneDirectory(3);

          // 可以返回任意未分配的號碼,這里我們假設它返回 0。
          directory.get();

          // 假設,函數(shù)返回 1。
          directory.get();

          // 號碼 2 未分配,所以返回為 true。
          directory.check(2);

          // 返回 2,分配后,只剩一個號碼未被分配。
          directory.get();

          // 此時,號碼 2 已經被分配,所以返回 false。
          directory.check(2);

          // 釋放號碼 2,將該號碼變回未分配狀態(tài)。
          directory.release(2);

          // 號碼 2 現(xiàn)在是未分配狀態(tài),所以返回 true。
          directory.check(2);


          解題

          https://www.cnblogs.com/grandyang/p/5735205.html


          又是一道設計題,讓我們設計一個電話目錄管理系統(tǒng),可以分配電話號碼,查詢某一個號碼是否已經被使用,釋放一個號碼。既然要分配號碼,肯定需要一個數(shù)組 nums 來存所有可以分配的號碼,注意要初始化成不同的數(shù)字。然后再用一個長度相等的數(shù)組 used 來標記某個位置上的號碼是否已經被使用過了,用一個變量 idx 表明當前分配到的位置。

          再 get 函數(shù)中,首先判斷若 idx 小于0了,說明沒有號碼可以分配了,返回 -1。否則就取出 nums[idx],并且標記該號碼已經使用了,注意 idx 還要自減1,返回之前取出的號碼。

          對于 check 函數(shù),直接在 used 函數(shù)中看對應值是否為0。最后實現(xiàn) release 函數(shù),若該號碼沒被使用過,直接 return;否則將 idx 自增1,再將該號碼賦值給 nums[idx],然后在 used 中標記為0即可,參見代碼如下:


          class PhoneDirectory {
          public:
              PhoneDirectory(int maxNumbers) {
                  nums.resize(maxNumbers);
                  used.resize(maxNumbers);
                  idx = maxNumbers - 1;
                  iota(nums.begin(), nums.end(), 0);
              }
              int get() {
                  if (idx < 0) return -1;
                  int num = nums[idx--];
                  used[num] = 1;
                  return num;
              }
              bool check(int number) {
                  return used[number] == 0;
              }
              void release(int number) {
                  if (used[number] == 0) return;
                  nums[++idx] = number;
                  used[number] = 0;
              }

          private:
              int idx;
              vector<int> nums, used;
          };


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-360題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)361:轟炸敵人

          LeetCode刷題實戰(zhàn)362:敲擊計數(shù)器

          LeetCode刷題實戰(zhàn)363:矩形區(qū)域不超過 K 的最大數(shù)值和
          LeetCode刷題實戰(zhàn)364:加權嵌套序列和 II
          LeetCode刷題實戰(zhàn)365:水壺問題
          LeetCode刷題實戰(zhàn)366:尋找二叉樹的葉子節(jié)點
          LeetCode刷題實戰(zhàn)367:有效的完全平方數(shù)
          LeetCode刷題實戰(zhàn)368:最大整除子集數(shù)
          LeetCode刷題實戰(zhàn)369:給單鏈表加一
          LeetCode刷題實戰(zhàn)370:區(qū)間加法
          LeetCode刷題實戰(zhàn)371:兩整數(shù)之和
          LeetCode刷題實戰(zhàn)372:超級次方
          LeetCode刷題實戰(zhàn)373:查找和最小的K對數(shù)字
          LeetCode刷題實戰(zhàn)374:猜數(shù)字大小
          LeetCode刷題實戰(zhàn)375:猜數(shù)字大小 II
          LeetCode刷題實戰(zhàn)376:擺動序列
          LeetCode刷題實戰(zhàn)377:組合總和 Ⅳ
          LeetCode刷題實戰(zhàn)378:有序矩陣中第 K 小的元素

          瀏覽 64
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  自拍偷拍视频网址 | 老女人操B视频 | 91黑人大屌啪啪 | www操一操 | 1000部成人无码视频 |