美文网首页
394. 字符串解码

394. 字符串解码

作者: 名字是乱打的 | 来源:发表于2021-12-20 22:55 被阅读0次

一 题目:

二 思路:

  • 利用辅助栈记录循环次数以及拼好的字符串
  • 利用count=count*10+(c-'0');解决数字可能是多位的问题

三代码:

class Solution {
        public String decodeString(String s) {
            //括号前的数量,用于做倍数计算
            LinkedList<Integer> countStack=new LinkedList<>();
            //字符串栈
            LinkedList<String> strStack=new LinkedList<>();
            StringBuilder sb=new StringBuilder();

            int count=0;
            //eg:输入:s = "3[a]2[bc]"
            for (char c : s.toCharArray()) {
                if (c>='0'&&c<='9'){
                    count=count*10+(c-'0');
                }else if (c>='a'&&c<='z'){
                    sb.append(c);
                }else if (c=='['){
                    countStack.push(count);
                    strStack.push(sb.toString());

                    sb=new StringBuilder();
                    count=0;
                }else {
                    //c==]
                    StringBuilder currSb=new StringBuilder();
                    currSb.append(strStack.pop());
                    Integer currCount = countStack.pop();
                    for (int i = 0; i < currCount; i++) {
                        currSb.append(sb);
                    }
                    sb=currSb;
                }
            }

        return sb.toString();
        }
    }

相关文章

  • 打卡-字符串解码

    394. 字符串解码

  • 394. 字符串解码

    394.字符串解码给定一个经过编码的字符串,返回它解码后的字符串。 编码规则为: k[encoded_string...

  • 394. 字符串解码

    394. 字符串解码 给定一个经过编码的字符串,返回它解码后的字符串。 编码规则为: k[encoded_stri...

  • LeetCode 394. 字符串解码 | Python

    394. 字符串解码 题目来源:力扣(LeetCode)https://leetcode-cn.com/probl...

  • 394.字符串解码

    ​394.字符串解码 题目分析 对这个题目的需求进行分析(需求分析来自Leetcode用户名为凛冬[1])我只是稍...

  • 394. 字符串解码

    题目描述 给定一个经过编码的字符串,返回它解码后的字符串。 编码规则为: k[encoded_string],表示...

  • 394.字符串解码

    执行用时 :1 ms, 在所有Java提交中击败了90.09%的用户 内存消耗 :37.4 MB, 在所有Java...

  • 394. 字符串解码

    给定一个经过编码的字符串,返回它解码后的字符串。 编码规则为: k[encoded_string],表示其中方括号...

  • 394. 字符串解码

    题目链接:https://leetcode-cn.com/problems/decode-string/ 解题思路...

  • 394. 字符串解码

    解法

网友评论

      本文标题:394. 字符串解码

      本文链接:https://www.haomeiwen.com/subject/baltqrtx.html