decode-string

Posted 笨鸟居士的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了decode-string相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/decode-string/

public class Solution {
    private String s;
    private int newPos;
    
    public String decodeString(String ins) {
        s = ‘.‘ + ins + ‘]‘;
        newPos = 0;
        String outStr = impl(1, 0);
        return outStr.substring(1, outStr.length());
    }
    
    private String impl(int prefix, int startPos) {
        int base = 0;
        String baseStr = "";
        String outStr = "";
        
        for (int i=startPos; i<s.length(); i++) {
            char ch = s.charAt(i);
            
            if (ch == ‘[‘) {
                int tmpPos = i+1;
                baseStr += impl(base, tmpPos);
                i = newPos;
                base = 0;
            }
            else if (ch == ‘]‘) {
                for (int j=0; j<prefix; j++) {
                    outStr += baseStr;
                }
                // At begin, use i+1, is wrong,
                // because in each loop there‘s i++
                newPos = i;
                return outStr;
            }
            else if (!Character.isDigit(ch)){
                baseStr += ch;
            }
            else {
                base = base * 10 + ch - ‘0‘;
            }
        }
        
        return outStr;
    }
}

 

以上是关于decode-string的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序代码片段

LeetCode 394. 字符串解码

LeetCode 394. 字符串解码

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?