<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)320:列舉單詞的全部縮寫

          共 3973字,需瀏覽 8分鐘

           ·

          2021-07-13 18:49

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

          今天和大家聊的問題叫做 列舉單詞的全部縮寫,我們先來看題面:
          https://leetcode-cn.com/problems/generalized-abbreviation/

          Write a function to generate the generalized abbreviations of a word.

          請你寫出一個能夠舉單詞全部縮寫的函數(shù)。
          注意:輸出的順序并不重要。


          示例


          示例:
          輸入: “word”
          輸出:
          [“word”, “1ord”, “w1rd”, “wo1d”, “wor1”, “2rd”, “w2d”, “wo2”, “1o1d”, “1or1”, “w1r1”, “1o2”, “2r1”, “3d”, “w3”, “4”]


          解題


          迭代的方法,在之前出現(xiàn)過的字符串的基礎(chǔ)上,接著更新新的可能的字符串的組成。


          class Solution {
          public:
              vector<string> generateAbbreviations(string word) {
                  int len=word.size();
                  vector<string> res;
                  res.push_back("");//初始結(jié)果
                  for(int i=0;i<len;++i){
                      string w=word.substr(i,1);//對每個字符位置進行處理
                      int nums_s=res.size();
                      //對之前出現(xiàn)過的每種結(jié)果進行更新
                      for(int j=0;j<nums_s;++j){
                          string tmp=res[j];
                          res[j]+=w;//更新加上當前字符的方式
                  
                  //更新轉(zhuǎn)成當前數(shù)字的方式
                          int k=tmp.size();
                          //找出當前字符串的末尾的數(shù)字
                          while(k>0&&isdigit(tmp[k-1])){
                              --k;
                          }
                          //根據(jù)當前字符串的末尾的字符是否是數(shù)字的情形,更新
                          if(k==tmp.size()){
                              res.push_back(tmp+"1");
                          }
                          else{
                              res.push_back(tmp.substr(0,k)+to_string(1+stoi(tmp.substr(k,tmp.size()-k))));
                          }
                      }
                  }
                  return res;
              }
          };



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

          上期推文:
          LeetCode1-300題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)301:刪除無效的括號
          LeetCode刷題實戰(zhàn)302:包含全部黑色像素的最小矩陣
          LeetCode刷題實戰(zhàn)303:區(qū)域和檢索 - 數(shù)組不可變
          LeetCode刷題實戰(zhàn)304:二維區(qū)域和檢索 - 矩陣不可變
          LeetCode刷題實戰(zhàn)305:島嶼數(shù)量II
          LeetCode刷題實戰(zhàn)306:累加數(shù)
          LeetCode刷題實戰(zhàn)307:區(qū)域和檢索 - 數(shù)組可修改
          LeetCode刷題實戰(zhàn)308:二維區(qū)域和檢索 - 可變
          LeetCode刷題實戰(zhàn)309:最佳買賣股票時機含冷凍期
          LeetCode刷題實戰(zhàn)310:最小高度樹
          LeetCode刷題實戰(zhàn)311:稀疏矩陣的乘法
          LeetCode刷題實戰(zhàn)312:戳氣球
          LeetCode刷題實戰(zhàn)313:超級丑數(shù)
          LeetCode刷題實戰(zhàn)314:二叉樹的豎直遍歷
          LeetCode刷題實戰(zhàn)315:計算右側(cè)小于當前元素的個數(shù)
          LeetCode刷題實戰(zhàn)316:去除重復(fù)字母
          LeetCode刷題實戰(zhàn)317:離建筑物最近的距離
          LeetCode刷題實戰(zhàn)318:最大單詞長度乘積
          LeetCode刷題實戰(zhàn)319:燈泡開關(guān)

          瀏覽 19
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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>
                  麻豆av影院日韩影院 | 在线三级播放 | 蜜臀AV一区二区三区有限公司 | 五月婷婷欧美激情 | 欧美伦理一区二区 |