<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刷題實(shí)戰(zhàn)318:最大單詞長(zhǎng)度乘積

          共 6422字,需瀏覽 13分鐘

           ·

          2021-07-13 18:51

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

          今天和大家聊的問題叫做 最大單詞長(zhǎng)度乘積,我們先來看題面:
          https://leetcode-cn.com/problems/maximum-product-of-word-lengths/

          Given a string array words, return the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. If no such two words exist, return 0.

          給定一個(gè)字符串?dāng)?shù)組 words,找到 length(word[i]) * length(word[j]) 的最大值,并且這兩個(gè)單詞不含有公共字母。你可以認(rèn)為每個(gè)單詞只包含小寫字母。如果不存在這樣的兩個(gè)單詞,返回 0。


          示例


          示例 1:

          輸入: ["abcw","baz","foo","bar","xtfn","abcdef"]
          輸出: 16 
          解釋: 這兩個(gè)單詞為 "abcw", "xtfn"

          示例 2:

          輸入: ["a","ab","abc","d","cd","bcd","abcd"]
          輸出: 4 
          解釋: 這兩個(gè)單詞為 "ab", "cd"

          示例 3:

          輸入: ["a","aa","aaa","aaaa"]
          輸出: 0 
          解釋: 不存在這樣的兩個(gè)單詞。


          解題

          https://blog.csdn.net/qq_41855420/article/details/88370883

          方法一:蠻力法。窮舉兩個(gè)字符串,然后判斷是否存在相同的字符,如果不存在則更新單詞長(zhǎng)度乘積。


          class Solution {
          public:
              int maxProduct(vector<string>& words) {
                  int result = 0;
                  int wordsSize = words.size();
                  //対第一個(gè)字符串窮舉
                  for (int nowIndex = 0; nowIndex < wordsSize; ++nowIndex){
                      //標(biāo)記第一個(gè)字符串各個(gè)字符出現(xiàn)
                      map<char, bool> firstMap;
                      for (auto ch : words[nowIndex]){
                          firstMap[ch] = true;
                      }
                      //窮舉第二個(gè)字符串
                      for (int afterIndex = nowIndex + 1; afterIndex < wordsSize; ++afterIndex){
                          int afterWordSize = words[afterIndex].size(), tempIndex = 0;
                          //判讀第一個(gè)、第二個(gè)字符串是否存在相同的字符
                          for (; tempIndex < afterWordSize; ++tempIndex){
                              if (firstMap[words[afterIndex][tempIndex]]){
                                  break;
                              }
                          }
                          //如果不存在相同的字符
                          if (afterWordSize == tempIndex){
                              result = max(int(afterWordSize * words[nowIndex].size()), result);//更新結(jié)果
                          }
                      }
                  }
                  return result;
              }
          };


          方法二:使用位操作。
          小寫字母一共有26,而int型有32位,所以每一個(gè)小寫字母占據(jù)一個(gè)位。在判斷字符是否存在相同的字符時(shí)只需要判斷是否存在某一位同時(shí)為1.

          class Solution {
          public:
              int maxProduct(vector<string>& words) {
                  int result = 0;
                  int wordsSize = words.size();
                  vector<int> wordToInt(wordsSize, 0);//wordToInt[i]表示words[i]按照字母分別占據(jù)以為得到的int數(shù)據(jù)
                  //対第一個(gè)字符串窮舉
                  for (int nowIndex = 0; nowIndex < wordsSize; ++nowIndex){
                      //將第一個(gè)字符串按照字符占據(jù)位,計(jì)算為int
                      for (auto ch : words[nowIndex]){
                          wordToInt[nowIndex] |= (1 << (ch - 'a'));
                      }
                      //窮舉第二個(gè)字符串
                      for (int afterIndex = 0; afterIndex < nowIndex; ++afterIndex){
                          //如果不存在相同的字符
                          if ((wordToInt[nowIndex] & wordToInt[afterIndex]) == 0){
                              result = max(result, int(words[nowIndex].size() * words[afterIndex].size()));
                          }
                      }
                  }
                  return result;
              }
          };


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

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

          瀏覽 35
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  狠狠综合久久 | 成人午夜福利视频 | 7777国产 | 色婷婷国产精品视频 | 伊人亚洲中文字幕 |