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.

  • 相关阅读:
    简单的纯css菜单
    提高 web 应用性能之 JavaScript 性能调优(转)
    从拖延者到行动派的10个秘诀(转)
    程序员,你应该知道(转)
    幽默的经济学+组织领导学
    [SQL基础]统计信息解释
    演讲集合
    最隐晦的程序设计指引(转)
    windows 7 "unmountable boot volume" 解决方法
    kafka与springboot集成2
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6751689.html
Copyright © 2011-2022 走看看