[leetcode]394. Decode String

本文介绍了解码字符串的算法实现,该算法通过栈来处理输入字符串中的数字和括号,重复括号内的字符串指定次数,最终返回解码后的完整字符串。文章提供了一个C++示例,演示如何处理如3[a]2[bc]这样的输入,输出aaabcbc。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:https://leetcode.com/problems/decode-string/

Given an encoded string, return it's 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].

Examples:

s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".

class Solution{
public:
    string decodeString(string s)
    {
        string ans;
        stack<string> S;
        stack<int> countStack;
        int i=0;
        while(i<s.length())
        {
            if(s[i]<='9' && s[i]>='0')
            {
                int count=0;
                while(s[i]<='9' && s[i]>='0')
                {
                    count=count*10+(s[i]-'0');
                    i++;
                }
                countStack.push(count);
            }
            else if (s[i]=='[')
            {
                S.push(ans);
                ans="";
                i++;
            }
            else if(s[i]==']')
            {
                string tmp=S.top();
                S.pop();
                int cnt=countStack.top();
                countStack.pop();
                for(int j=0;j<cnt;j++)
                    tmp+=ans;
                ans=tmp;
                i++;
            }
            else
            {
                ans+=s[i];
                i++;
            }
        }
        return ans;
    }
};


leetcode394题是"字符串解码"(Decode String)。 题目描述: 给定一个经过编码的字符串,返回它解码后的字符串。 编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。 可以保证输入字符串总是有效的,没有额外的空格,且输入的方括号格式始终正确。 示例: 输入:s = "3[a]2[bc]" 输出:"aaabcbc" 解释: "3[a]2[bc]" 解码后为 "aaabcbc"。 解题思路: 我们可以使用栈来解决这个问题。遍历输入字符串 s 的每个字符 c: 1. 当 c 为数字时,将数字字符转化为数字 multi,用于后续倍数计算; 2. 当 c 为字母时,在 res 尾部添加 c; 3. 当 c 为左括号时,将当前 multi 和 res 入栈,并分别置空置 0: - 记录此 [ 前的临时结果 res 至栈,用于发现对应 ] 后的拼接操作; - 记录此 [ 前的倍数 multi 至栈,用于发现对应 ] 后,获取 multi × [...] 字符串。 进入到新 [ 后,multi 和 res 重新记录。 4. 当 c 为右括号时,stack 出栈,拼接字符串 res = last_res + cur_multi * res, last_res 是上个 [ 到当前 [ 的字符串,cur_multi 是当前 [ 到 ] 内字符串的重复倍数。 5. 遍历结束后返回结果 res。 代码实现如下: ```python def decodeString(s: str) -> str: stack = [] res = "" multi = 0 for c in s: if '0' <= c <= '9': multi = multi * 10 + int(c) elif c == '[': stack.append((multi, res)) multi = 0 res = "" elif c == ']': cur_multi, last_res = stack.pop() res = last_res + cur_multi * res else: res += c return res ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值