<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      [LeetCode] 1370. Increasing Decreasing String 上升下降字符串


      You are given a string s. Reorder the string using the following algorithm:

      1. Remove the smallest character from s and append it to the result.
      2. Remove the smallest character from s that is greater than the last appended character, and append it to the result.
      3. Repeat step 2 until no more characters can be removed.
      4. Remove the largest character from s and append it to the result.
      5. Remove the largest character from s that is smaller than the last appended character, and append it to the result.
      6. Repeat step 5 until no more characters can be removed.
      7. Repeat steps 1 through 6 until all characters from s have been removed.

      If the smallest or largest character appears more than once, you may choose any occurrence to append to the result.

      Return the resulting string after reordering s using this algorithm.

      Example 1:

      Input: s = "aaaabbbbcccc"
      Output: "abccbaabccba"
      Explanation: After steps 1, 2 and 3 of the first iteration, result = "abc"
      After steps 4, 5 and 6 of the first iteration, result = "abccba"
      First iteration is done. Now s = "aabbcc" and we go back to step 1
      After steps 1, 2 and 3 of the second iteration, result = "abccbaabc"
      After steps 4, 5 and 6 of the second iteration, result = "abccbaabccba"
      

      Example 2:

      Input: s = "rat"
      Output: "art"
      Explanation: The word "rat" becomes "art" after re-ordering it with the mentioned algorithm.
      

      Constraints:

      • 1 <= s.length <= 500
      • s consists of only lowercase English letters.

      這道題說是給了一個字符串s,讓采取下面一系列的措施:

      1. 刪除s中最小的字符并將其附加到結(jié)果中。
      2. 刪除比上次附加的字符大的s中最小的字符,并將其附加到結(jié)果中。
      3. 重復步驟2,直到不能再刪除任何字符。
      4. 刪除s中最大的字符并將其附加到結(jié)果中。
      5. 刪除比上次附加的字符小的s中最大的字符,并將其附加到結(jié)果中。
      6. 重復步驟5,直到不能再刪除任何字符。
      7. 重復步驟1到6,直到所有字符從s中被刪除。

      那么實際上就是找每個狀態(tài)下s中的最大或最小的字符以及其個數(shù),所以就需要統(tǒng)計每個字符出現(xiàn)的次數(shù),而且最好還要能很方便的知道最大最小的字符是什么。一般來說,如果只想統(tǒng)計每個字符出現(xiàn)的個數(shù),會用 HashMap 來做,但是這道題明確說了需要知道最大和最小的字符,則就應該用 TreeMap 或者數(shù)組來做。這里用數(shù)組來做更方便一點,因為題目中限定了字符串s中只有 26 個小寫字母,則用一個大小為 26 的數(shù)組來統(tǒng)計每個字母的出現(xiàn)次數(shù)即可,而遍歷的方向就可以決定取最大或最小的字符。由于總體步驟是要循環(huán)執(zhí)行的,所以最外層要套一個 while 循環(huán),判定條件就是結(jié)果 res 的長度不等于原字符串s。

      然后從頭開始遍歷統(tǒng)計字符個數(shù)的數(shù)組 charCnt,若某個字符個數(shù)自減1之后仍大于等于0,則說明該字符存在,并且是當前最大的字符,則將其加入結(jié)果 res 中,這樣保證了每種字符只會取一個,這樣一圈遍歷下來步驟1至3就完成了。同理,從末尾往前遍歷,若某個字符個數(shù)自減1之后仍大于等于0,則說明該字符存在,并且是當前最小的字符,則將其加入結(jié)果 res 中,這樣保證了每種字符只會取一個,這樣一圈遍歷下來步驟4至6就完成了。同時最外層的循環(huán)保證了步驟1至6可以重復執(zhí)行,最終循環(huán)退出后就得到符合要求的結(jié)果 res,參見代碼如下:


      class Solution {
      public:
          string sortString(string s) {
              string res;
              vector<int> charCnt(26);
              for (char c : s) {
                  ++charCnt[c - 'a'];
              }
              while (s.size() != res.size()) {
                  for (int i = 0; i < 26; ++i) {
                      if (--charCnt[i] >= 0) {
                          res += (i + 'a');
                      }
                  }
                  for (int i = 25; i >= 0; --i) {
                      if (--charCnt[i] >= 0) {
                          res += (i + 'a');
                      }
                  }
              }
              return res;
          }
      };
      

      Github 同步地址:

      https://github.com/grandyang/leetcode/issues/1370


      參考資料:

      https://leetcode.com/problems/increasing-decreasing-string

      https://leetcode.com/problems/increasing-decreasing-string/solutions/533002/c-counts/


      LeetCode All in One 題目講解匯總(持續(xù)更新中...)

      posted @ 2025-01-18 19:26  Grandyang  閱讀(125)  評論(0)    收藏  舉報
      Fork me on GitHub
      主站蜘蛛池模板: 国产最新AV在线播放不卡| 亚洲人成网线在线播放VA | 色吊丝免费av一区二区| 秋霞电影院午夜无码免费视频| 国产精品污双胞胎在线观看| 亚洲蜜臀av乱码久久| 久久中文字幕av第二页| 国产日韩一区二区在线看| 亚洲欧美在线综合一区二区三区| 历史| 色老99久久精品偷偷鲁| 亚洲性美女一区二区三区| 好爽好紧好大的免费视频| 普定县| 日韩国产中文字幕精品| 日韩区中文字幕在线观看| 国产稚嫩高中生呻吟激情在线视频| 日本55丰满熟妇厨房伦| 亚洲少妇人妻无码视频| 久久大香萑太香蕉av黄软件 | 亚洲精品天天影视综合网| 国产天堂亚洲国产碰碰| 国产极品精品自在线不卡| 亚洲一级特黄大片一级特黄| 国产精品亚洲一区二区三区| 久青草国产在视频在线观看| 激情的视频一区二区三区| 深夜在线观看免费av| 国产欧美日韩精品丝袜高跟鞋| 亚洲无码精品视频| 亚洲av精选一区二区| 成人国产精品一区二区网站公司 | 亚洲国产精品午夜福利| 偷炮少妇宾馆半推半就激情| 国产成人综合在线观看不卡| 欧美亚洲综合久久偷偷人人| 护士张开腿被奷日出白浆| 国产亚洲精品久久久久5区| 1区2区3区4区产品不卡码网站| 蜜臀av一区二区精品字幕| 久久这里只有精品免费首页|