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;
    }
}
时间: 2024-10-28 23:24:20

decode-string的相关文章

[Swift]LeetCode394. 字符串解码 | Decode String

Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where the encoded_stringinside the square brackets is being repeated exactly k times. Note that kis guaranteed to be a positive integer. You may assume that

LeetCode-394. Decode String(DFS)

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 th

394. 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 thatk is guaranteed to be a positive integer. You may assume tha

[LeetCode] 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 th

Leetcode 394: 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 th

394. Decode String 解码icc字符串3[i2[c]]

[抄题]: 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 ass

[LC] 394. Decode String

Given an encoded string, return its 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 tha

Python 解LeetCode:394 Decode String

题目描述:按照规定,把字符串解码,具体示例见题目链接 思路:使用两个栈分别存储数字和字母 注意1: 数字是多位的话,要处理后入数字栈 注意2: 出栈时过程中产生的组合后的字符串要继续入字母栈 注意3: 记得字母出栈的时候字符要逆序组合成字符串 注意4: 不用字符串而用字母栈的原因是字符串的 join 效率会比字符串加法高一些 结果: 30 ms, beat 98.02% 缺点:判断是数字那里有点代码不简洁,可以把 j 挪到循环外面的 class Solution(object): def dec

URL地址中中文乱码详解(javascript中encodeURI和decodeURI方法、java.net.URLDecoder.encode、java.net.URLDecoder.decode)

引言: 在Restful类的服务设计中,经常会碰到需要在URL地址中使用中文作为的参数的情况,这种情况下,一般都需要正确的设置和编码中文字符信息.乱码问题就此产生了,该如何解决呢?且听本文详细道来. 1.  问题的引出 在Restful的服务设计中,查询某些信息的时候,一般的URL地址设计为: get /basic/service? keyword=历史 , 之类的URL地址. 但是,在实际的开发和使用中,确是有乱码情况的发生,在后台的读取keyword信息为乱码,无法正确读取. 2. 乱码是如

java 随机生成一个中文、判断某个string是否是中文以及打印出全部的中文

现在网上大多数用于判断中文字符的是 U+4E00..U+9FA5 这个范围是只是"中日韩统一表意文字"这个区间,但这不是全部,如果要全部包含,则还要他们的扩展集.部首.象形字.注间字母等等; 2E80-A4CF: 包含了中日朝部首补充.康熙部首.表意文字描述符.中日朝符号和标点.日文平假名.日文片假名.注音字母.谚文兼容字母.象形字注释标志.注音字母扩展.中日朝笔画.日文片假名语音扩展.带圈中日朝字母和月份.中日朝兼容.中日朝统一表意文字扩展A.易经六十四卦符号.中日韩统一表意文字.彝