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 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 = "xyz3[a12[cb]]", return "....."

Solution1:two Stacks (count, result)

思路:到数字时,将原来的res存入res_stack,数字存入count_stack,res重置 ,继续处理后序。碰到]时,将res_stack.pop() + res * count_stack.pop() times,继续处理后序。
Time Complexity: O(N) Space Complexity: O(N)

Solution2:Recursive

参考:https://discuss.leetcode.com/topic/57228/0ms-simple-c-solution
Time Complexity: O(N) Space Complexity: O(N) 递归缓存

Solution1 Code:

public class Solution {
    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;
    }
}

Solution2 Code:

public class Solution {
    public String decodeString(String s) {
        StringBuilder result = decode(s, new int[] {0});
        return result.toString();
    }
    
    // start: //so that it can be changed. (like reference)
    private StringBuilder decode(String s, int[] start) {
        StringBuilder result = new StringBuilder();
        
        while(start[0] < s.length() && s.charAt(start[0]) != ']') {
            if(Character.isDigit(s.charAt(start[0]))) {
                int count = 0;
                while (Character.isDigit(s.charAt(start[0]))) {
                    count = 10 * count + (s.charAt(start[0]) - '0');
                    start[0]++;
                }
                
                start[0]++; // ']'
                StringBuilder post = decode(s, start);
                start[0]++; // ']'
                
                while(count > 0) {
                    result.append(post);
                    count--;
                }
                
            }
            else {
                result.append(s.charAt(start[0]));
                start[0]++;
            }
        }
        return result;
    }
}

Round1

class Solution {
    public String decodeString(String s) {
        
        Deque<String> stack_str = new ArrayDeque<>();
        Deque<Integer> stack_count = new ArrayDeque<>();
        
        int i = 0;
        
        StringBuilder str = new StringBuilder("");
        while(i < s.length()) {  
            if(Character.isDigit(s.charAt(i))) {
                int num = 0;
                while(Character.isDigit(s.charAt(i))) {
                    num = num * 10 + s.charAt(i) - '0';
                    i++;
                }
                stack_count.push(num);
            }
            
            else if(s.charAt(i) == '[') {
                stack_str.push(str.toString());
                str = new StringBuilder("");
                i++;
            }
            else if(s.charAt(i) == ']') {
                StringBuilder prev = new StringBuilder(stack_str.pop());
                int times = stack_count.pop();
                for(int t = 0; t < times; t++) {
                    prev.append(str);
                }
                str = prev;
                i++;
            }
            else {
                // regular string
                str.append(s.charAt(i));
                i++;
            }
        }
        return str.toString();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 宝玉是个理想主义者,他眼中的世界应该是纯洁无暇的。 宝玉认为官场是虚伪的,人之所以想做官只是为了争权夺...
    婉㚥阅读 3,310评论 5 14
  • 2016.10.31。万圣节前的一晚。j迎来了家长的第一个电话。对于一位幼儿教师,这样的时间这,本是一件再平常不...
    蒋j蒋阅读 163评论 0 0
  • 生活中其实我们不需要那么多东西,人类越发依赖外物反而束缚了自己的手脚,中国儒学的“度”真是玄妙。 ——逆光2017
    逆光2017阅读 154评论 0 0