LeetCode394 解码字符串

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”.

string本身是不可改变的,它只能赋值一次,每一次内容发生改变,都会生成一个新的对象,所以用stringbuilder

public String decodeString(String s) {
        String res = "";
        Stack<Integer> countStack = new Stack<>();
        Stack<String> resStack = new Stack<>();
        int idx = 0;
        while (idx < s.length()) {
            if (Character.isDigit(s.charAt(idx))) {
                int count = 0;
                while (Character.isDigit(s.charAt(idx))) {
                    count = 10 * count + (s.charAt(idx) - '0');
                    idx++;
                }
                countStack.push(count);
            }
            else if (s.charAt(idx) == '[') {
                resStack.push(res);
                res = "";
                idx++;
            }
            else if (s.charAt(idx) == ']') {
               StringBuilder temp = new StringBuilder (resStack.pop());
                int repeatTimes = countStack.pop();
                for (int i = 0; i < repeatTimes; i++) {
                    temp.append(res);
                }
                res = temp.toString();
                idx++;
            }
            else {
                res += s.charAt(idx++);
            }
        }
        return res;
    }
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、付费专栏及课程。

余额充值