<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)248:中心對稱數(shù)III

          共 3824字,需瀏覽 8分鐘

           ·

          2021-04-28 13:22

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

          今天和大家聊的問題叫做 中心對稱數(shù)III,我們先來看題面:
          https://leetcode-cn.com/problems/strobogrammatic-number-iii/

          A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).


          Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high.

          中心對稱數(shù)是指一個數(shù)字在旋轉了 180 度之后看起來依舊相同的數(shù)字(或者上下顛倒地看)。
          寫一個函數(shù)來計算范圍在 [low, high] 之間中心對稱數(shù)的個數(shù)。

          示例


          示例:
          輸入: low = “50”, high = “100”
          輸出: 3
          解釋: 69,88 和 96 是三個在該范圍內的中心對稱數(shù)


          解題

          主要思路:

          (1)仔細觀察中心對稱數(shù)的規(guī)律,若中心對稱數(shù)的位數(shù)是偶數(shù),則可以由{0,0}{1,1}{6,9}{9,6}{8,8}從數(shù)字的兩端對稱組成,若中心對稱數(shù)的位數(shù)是奇數(shù),則中心數(shù)字只能是0,1,8三種數(shù)字;

          (2)根據上述的分析,可以從空字符串,或“1”,“0”,“8”開始,在字符串的兩端對應的添加數(shù)字對,來實現(xiàn)中心對稱數(shù)的構成;

          (3)在構造中心對稱數(shù)的過程中,和low,high比較,保證生成的中心對稱數(shù)是有效的;

          class Solution {
          public:
            //使用深度優(yōu)先構造中心對稱數(shù)
              void dfs(string& low,string& high,int& count,string cur){
                  if(cur.size()>=low.size()&&cur.size()<=high.size()){//若當前的中心對稱數(shù)在有效范圍之內
                    //三種當前的中心對稱數(shù)是無效的情形
                    //第一種是當前的中心對稱數(shù)的長度符合要求,但大于最大值
                    //第二種是當前的中心對稱數(shù)的長度符合要求,但小于最小值
                    //第三種是當前的中心對稱數(shù)的長度大于1,但最高位是0的情形
                    //若非這三種情形,則說明當前的中心對稱數(shù)是有效的數(shù),統(tǒng)計數(shù)量加1
                      if(!(cur.size()==high.size()&&cur>high
                      ||cur.size()==low.size()&&cur<low
                      ||cur.size()>=2&&cur[0]=='0')){
                          ++count;
                      }
                  }
                  //若長度越界,則返回
                  if(cur.size()+2>high.size()){
                      return;
                  }
                  //幾種從當前情形生成新的中心對稱數(shù)的方式
                  dfs(low,high,count,"0"+cur+"0");
                  dfs(low,high,count,"1"+cur+"1");
                  dfs(low,high,count,"6"+cur+"9");
                  dfs(low,high,count,"9"+cur+"6");
                  dfs(low,high,count,"8"+cur+"8");
              }
              int strobogrammaticInRange(string low, string high) {
                  int count=0;//統(tǒng)計中心對稱數(shù)的個數(shù)
                  //幾種中心對稱數(shù)的可能的初始情形
                  dfs(low,high,count,"");
                  dfs(low,high,count,"1");
                  dfs(low,high,count,"0");
                  dfs(low,high,count,"8");
                  return count;
              }
          };


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

          上期推文:

          LeetCode1-240題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)241:為運算表達式設計優(yōu)先級
          LeetCode刷題實戰(zhàn)242:有效的字母異位詞
          LeetCode刷題實戰(zhàn)243:最短單詞距離
          LeetCode刷題實戰(zhàn)244:最短單詞距離 II
          LeetCode刷題實戰(zhàn)245:最短單詞距離 III
          LeetCode刷題實戰(zhàn)246:中心對稱數(shù)
          LeetCode刷題實戰(zhàn)247:中心對稱數(shù)II

          瀏覽 140
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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>
                  娇小小小泬BBB亚洲 | 成人黄色av | 操B视频网址 | 一区二区三区四区无码高清 | 日韩欧美mv国产 |