zoukankan      html  css  js  c++  java
  • LeetCode 394. Decode String

    原题链接在这里:https://leetcode.com/problems/decode-string/#/description

    题目:

    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 = "3[a2[c]]", return "accaccacc".
    s = "2[abc]3[cd]ef", return "abcabccdcdcdef".

    题解:

    当s的长度小于4的时候可以直接返回s, 因为缩写的s格式k[encoded_string]最小长度是4.

    利用两个Stack完成DFS. stringStk 保存遇到缩写前上层的String值, intStk保存当前层应该repeat的次数.

    遇到数字, 就说明遇到缩写了, 把需要repeat的次数先保存进inStk中. 把上一层的string保存到stringStk中.

    遇到"]"说明缩写结束. 把上一层的string 从stingStk中pop出来,后面append上repeat次数的当前层string.

    Time Complexity: O(s.length()). Space: O(s.length()), s是缩写的极致时用了最大的Stack.

    AC Java:

     1 public class Solution {
     2     public String decodeString(String s) {
     3         if(s == null || s.length() < 4){
     4             return s;
     5         }
     6         StringBuilder res = new StringBuilder();
     7         Stack<String> stringStk = new Stack<String>();
     8         Stack<Integer> intStk = new Stack<Integer>();
     9         int index = 0;
    10         while(index < s.length()){
    11             if(Character.isDigit(s.charAt(index))){
    12                 int n = 0;
    13                 while(index<s.length() && Character.isDigit(s.charAt(index))){
    14                     n = n*10 + (int)(s.charAt(index)-'0');
    15                     index++;
    16                 }
    17                 intStk.push(n);
    18                 stringStk.push(res.toString());
    19                 res = new StringBuilder();
    20                 index++; //跳过"["
    21             }else if(s.charAt(index) == ']'){
    22                 String formerRes = stringStk.pop();
    23                 StringBuilder sb = new StringBuilder(formerRes);
    24                 int n = intStk.pop();
    25                 while(n-- > 0){
    26                     sb.append(res);
    27                 }
    28                 res = sb;
    29                 index++;
    30             }else{
    31                 res.append(s.charAt(index++));
    32             }
    33         }
    34         return res.toString();
    35     }
    36 }

    DFS recursion 方法, 终止条件有两个, 遇到了']', 或者到了s的末位.

    Time Complexity: O(s.length()). Space: O(s.length()).

    AC Java:

     1 public class Solution {
     2     int i = 0;
     3     public String decodeString(String s) {
     4         StringBuilder sb = new StringBuilder();
     5         while(i<s.length()){
     6             if(Character.isDigit(s.charAt(i))){
     7                 int n = 0;
     8                 while(i<s.length() && Character.isDigit(s.charAt(i))){
     9                     n = n*10 + (int)(s.charAt(i)-'0');
    10                     i++;
    11                 }
    12                 
    13                 i++; // skip '['
    14                 String nested = decodeString(s);
    15                 
    16                 while(n-- > 0){
    17                     sb.append(nested);
    18                 }
    19             }else if(Character.isLetter(s.charAt(i))){
    20                 sb.append(s.charAt(i++));
    21             }else if(s.charAt(i) == ']'){
    22                 i++;
    23                 return sb.toString();
    24             }
    25         }
    26         return sb.toString();
    27     }
    28 }

    类似Brace Expansion.

  • 相关阅读:
    VBA宏-转载记录备份 2021年5月21日 星期五
    (记)利用Word发布文章到cnblogs博客
    赖氏经典英语语法—虚拟语气
    MFC进阶教程深入浅出版.笔记第5天
    MFC进阶教程深入浅出版.笔记第4天
    MFC进阶教程深入浅出版.笔记第3天
    MFC进阶教程深入浅出版.笔记第2天
    MFC进阶教程深入浅出版.笔记第1天
    介词7:during, through, besides, since…
    2.无人机无人车轨迹优化分类
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6751689.html
Copyright © 2011-2022 走看看