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,260評論 5 14
  • 2016.10.31。萬圣節前的一晚。j迎來了家長的第一個電話。對于一位幼兒教師,這樣的時間這,本是一件再平常不...
    蔣j蔣閱讀 162評論 0 0
  • 生活中其實我們不需要那么多東西,人類越發依賴外物反而束縛了自己的手腳,中國儒學的“度”真是玄妙。 ——逆光2017
    逆光2017閱讀 149評論 0 0