Leetcode394——Decode String

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. 问题描述

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

中文

给定一个经过编码的字符串,返回其解码字符串。编码规则为:k[encoded_string],其中中括号内的encoded_string被重复k次。注意k一定是正整数。

2. 求解

本题中明显有括号的匹配问题,因此需要使用栈来求解。当碰到右括号(])时,字符串出栈,碰到左括号([)时,保存左右括号内的字符串([]),继续出栈,保存字符串重复次数,直至栈为空或碰到非数字。要注意重复次数不是个位数,将字符串重复之后压入栈中。继续处理剩余字符串,同样执行上述过程,直至处理完字符串。然后将栈中所有的字符出栈构成结果字符串返回。

public class Solution {
    public String decodeString(String s) {
        int n = s.length();
        Stack<Character> stack = new Stack<Character>();
        String result = "";
        String temp = "";
        for (int i = 0; i < n; i ++) {
            char str = s.charAt(i);
            if (str != ']') {
                stack.push(str);
            } else {
                char ch = stack.pop();
                while (ch != '[') {
                    temp = ch + temp;
                    ch = stack.pop();
                }
                //字符串重复次数
                String times = "";
                while(!stack.isEmpty()) {
                    ch = stack.pop();
                    if(Character.isDigit(ch)) {
                        times = ch + times;
                    }else {
                        stack.push(ch);
                        break;
                    }
                }
                //重复字符串,压入栈中
                for(int j = 0; j < Integer.parseInt(times); j++) {
                    for(int k = 0; k < temp.length(); k++) {
                        stack.push(temp.charAt(k));
                    }
                }
                temp = "";
            }
        }
        while(!stack.isEmpty()) {
            result = stack.pop() + result;
        } 
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值