1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實(shí)戰(zhàn)394:字符串解碼

        共 3372字,需瀏覽 7分鐘

         ·

        2021-10-01 23:52

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

        今天和大家聊的問題叫做 字符串解碼,我們先來看題面:
        https://leetcode-cn.com/problems/decode-string/



        Given an encoded string, return its decoded string.
        The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
        You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
        Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4].

        給定一個(gè)經(jīng)過編碼的字符串,返回它解碼后的字符串。

        編碼規(guī)則為: k[encoded_string],表示其中方括號(hào)內(nèi)部的 encoded_string 正好重復(fù) k 次。注意 k 保證為正整數(shù)。

        你可以認(rèn)為輸入字符串總是有效的;輸入字符串中沒有額外的空格,且輸入的方括號(hào)總是符合格式要求的。

        此外,你可以認(rèn)為原始數(shù)據(jù)不包含數(shù)字,所有的數(shù)字只表示重復(fù)的次數(shù) k ,例如不會(huì)出現(xiàn)像 3a 或 2[4] 的輸入。

        示例


        示例 1
        輸入:s = "3[a]2[bc]"
        輸出:"aaabcbc"


        示例 2
        輸入:s = "3[a2[c]]"
        輸出:"accaccacc"


        示例 3
        輸入:s = "2[abc]3[cd]ef"
        輸出:"abcabccdcdcdef"


        示例 4
        輸入:s = "abc3[cd]xyz"
        輸出:"abccdcdcdxyz"


        解題

        https://blog.csdn.net/qq_30057549/article/details/103916466

        這題因?yàn)槔ㄌ?hào)的嵌套,很容易想到用遞歸或者棧,但是實(shí)際寫起來是比較復(fù)雜的,不好想出,需要多加思考。關(guān)鍵在于,如何保存前面已經(jīng)生成(棧中沒有左括號(hào))的字符串的信息。另外,最后有個(gè)小技巧,mul*10的使用,巧妙的按字符讀取n位整數(shù)。

        class Solution:
            def decodeString(self, s: str) -> str:
                #思路:這種類型的題,一種思路是棧,一種思路是遞歸。
                #本解法用的是棧,當(dāng)遇到[時(shí),我們將數(shù)字壓入棧中,并開始用cur_str記錄中括號(hào)里的字母,如遇到],就將之前的數(shù)字出棧并與當(dāng)前的cur_str相乘。
                mul, cur_str = 0, ''
                stack = []
                for c in s:
                    if c == '[':
                        stack.append((mul, cur_str))
                        mul, cur_str = 0, ''
                    elif c == ']':
                        n, pre = stack.pop()
                        cur_str = pre + n * cur_str
                    elif '0' <= c <= '9':
                        mul = 10 * mul + int(c)
                    else: #字母
                        cur_str += c
                return cur_str


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

        上期推文:

        LeetCode1-380題匯總,希望對(duì)你有點(diǎn)幫助!

        LeetCode刷題實(shí)戰(zhàn)381:O(1) 時(shí)間插入、刪除和獲取隨機(jī)元素

        LeetCode刷題實(shí)戰(zhàn)382:鏈表隨機(jī)節(jié)點(diǎn)

        LeetCode刷題實(shí)戰(zhàn)383:贖金信

        LeetCode刷題實(shí)戰(zhàn)384:打亂數(shù)組

        LeetCode刷題實(shí)戰(zhàn)385:迷你語法分析器

        LeetCode刷題實(shí)戰(zhàn)386:字典序排數(shù)
        LeetCode刷題實(shí)戰(zhàn)387:字符串中的第一個(gè)唯一字符
        LeetCode刷題實(shí)戰(zhàn)388:文件的最長絕對(duì)路徑
        LeetCode刷題實(shí)戰(zhàn)389:找不同
        LeetCode刷題實(shí)戰(zhàn)390:消除游戲
        LeetCode刷題實(shí)戰(zhàn)391:完美矩形
        LeetCode刷題實(shí)戰(zhàn)392:判斷子序列
        LeetCode刷題實(shí)戰(zhàn)393:UTF-8 編碼驗(yàn)證

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

        手機(jī)掃一掃分享

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

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            91综合在线 | 亚洲高清无码一区 | 日日日日日日bbbbb视频 日本男插女 | 成人无码电影在线观看 | 一级片操逼视频 | 色五月婷婷av | av淫色 | 国产色噜噜噜在线观看精品 | 国产毛片AV一区二区三区牛牛影视 | 日本视频久久 |