<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)424:替換后的最長重復(fù)字符

          共 1676字,需瀏覽 4分鐘

           ·

          2021-11-04 14:07

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

          今天和大家聊的問題叫做?替換后的最長重復(fù)字符,我們先來看題面:
          https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/

          You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times.


          Return the length of the longest substring containing the same letter you can get after performing the above operations.


          給你一個僅由大寫英文字母組成的字符串,你可以將任意位置上的字符替換成另外的字符,總共可最多替換 k 次。在執(zhí)行上述操作后,找到包含重復(fù)字母的最長子串的長度。
          注意:字符串長度 和 k 不會超過 10的4次方。

          示例


          示例 1
          輸入:s = "ABAB", k?= 2
          輸出:4
          解釋:用兩個'A'替換為兩個'B',反之亦然。

          示例 2
          輸入:s = "AABABBA", k?= 1
          輸出:4
          解釋:
          將中間的一個'A'替換為'B',字符串變?yōu)?"AABBBBA"
          子串 "BBBB"?有最長重復(fù)字母, 答案為 4。


          解題

          思路 :
          由于題目是問最長子串,這種題目動態(tài)規(guī)劃或者滑動窗口需要著重考慮一下,因為都是當(dāng)前狀態(tài)都是和之前相鄰的狀態(tài)有關(guān)。

          1.先固定左邊界,右邊界一直右移,直到當(dāng)前區(qū)間內(nèi)的需要替換的字母數(shù)大于k。
          2.之后左邊界右移,直到該區(qū)間需要替換的字母數(shù)小于等于k。重復(fù)1步驟。

          問題是怎么找出某個區(qū)間內(nèi)需要替換的字母數(shù)。

          答案是用一個maxCnt數(shù)組記錄各字母在當(dāng)前區(qū)間內(nèi)的出現(xiàn)次數(shù),出現(xiàn)最多的,我們認(rèn)為它就是主要字母,其他字母都替換為它。這樣總的替換次數(shù)是最少的。

          class?Solution?{
          public:
          ????int?characterReplacement(string?s, int?k)?{
          ????????vector<int> counts(26, 0); //記錄當(dāng)前窗口字母出現(xiàn)的個數(shù)
          ????????int?left=0, res=0, maxCnt=0; // maxCnt記錄字符出現(xiàn)次數(shù)最多那個字符 的次數(shù)
          ????????for(int?i=0; i????????????counts[s[i]-'A']++;
          ????????????maxCnt = max(maxCnt, counts[s[i]-'A']); // 比較之前記錄的最大數(shù) 和 當(dāng)前字符的數(shù)量
          ????????????while(i-left+1-maxCnt > k){ // 若當(dāng)前窗口大小 減去 窗口中最多相同字符的個數(shù) 大于 k 時
          ????????????????counts[s[left]-'A']--; // 將窗口最左邊的字符 在計數(shù)數(shù)組中減1
          ????????????????left++; // 滑動窗口
          ????????????}
          ????????????res = max(res, i-left+1);
          ????????}
          ????????return?res;
          ????}
          };


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

          上期推文:


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

          LeetCode刷題實戰(zhàn)421:數(shù)組中兩個數(shù)的最大異或值

          LeetCode刷題實戰(zhàn)422:有效的單詞方塊

          LeetCode刷題實戰(zhàn)423:從英文中重建數(shù)字


          瀏覽 29
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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>
                  无码性生活| 9999热这里只有精品 | 高清无码扣逼视频 | 国产精品无码在线 | 人人摸人人艹人人骑 |