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 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".
  1 public class Solution {
  2     public string DecodeString(string s)
  3     {
  4         if (s == null || s.Length == 0) return "";
  5
  6         var counts = new Stack<int>();
  7         var results = new Stack<string>();
  8
  9         int i = 0;
 10         results.Push("");
 11
 12         while (i < s.Length)
 13         {
 14             char c = s[i];
 15
 16             if (Char.IsDigit(c))
 17             {
 18                 int count = (int)c - (int)‘0‘;
 19                 while (i + 1 < s.Length && Char.IsDigit(s[i + 1]))
 20                 {
 21                     count = count * 10 + (int)s[i + 1] - (int)‘0‘;
 22                     i++;
 23                 }
 24
 25                 counts.Push(count);
 26             }
 27             else if (c == ‘[‘)
 28             {
 29                 results.Push("");
 30             }
 31             else if (c == ‘]‘)
 32             {
 33                 string str = results.Pop();
 34                 int count = counts.Pop();
 35
 36                 var sb = new StringBuilder();
 37
 38                 for (int k = 0; k < count; k++)
 39                 {
 40                     sb.Append(str);
 41                 }
 42
 43                 results.Push(results.Pop() + sb.ToString());
 44             }
 45             else
 46             {
 47                 results.Push(results.Pop() + c);
 48             }
 49
 50             i++;
 51         }
 52
 53         return results.Pop();
 54     }
 55 }
 56
 57 // dfs, hard to implement
 58 public class Solution1 {
 59     public string DecodeString(string s)
 60     {
 61         if (s == null || s.Length == 0) return "";
 62
 63         return ParseString(s, 0, s.Length);
 64     }
 65
 66     private string ParseString(string s, int start, int end)
 67     {
 68         if (start >= end) return "";
 69
 70         int i = start;
 71         while (i < end && !Char.IsDigit(s[i]))
 72         {
 73             i++;
 74         }
 75
 76         string pre = s.Substring(start, i - start);
 77
 78         int count = 0;
 79         while (i < end && Char.IsDigit(s[i]))
 80         {
 81             count = count * 10 + (int)s[i] - (int)‘0‘;
 82             i++;
 83         }
 84
 85         int strStart = ++i;
 86         int notClosed = 1, max = 1;
 87         var str = "";
 88
 89         while (i < end)
 90         {
 91             if (s[i] == ‘[‘)
 92             {
 93                 notClosed++;
 94             }
 95             else if (s[i] == ‘]‘)
 96             {
 97                 notClosed--;
 98             }
 99
100             max = Math.Max(max, notClosed);
101
102             if (notClosed == 0)
103             {
104                 if (max == 1)
105                 {
106                     str = s.Substring(strStart, i - strStart);
107                 }
108                 else
109                 {
110                     str = ParseString(s, strStart, i);
111                 }
112
113                 break;
114             }
115
116             i++;
117         }
118
119         var sb = new StringBuilder();
120
121         for (int k = 0; k < count; k++)
122         {
123             sb.Append(str);
124         }
125
126         return pre + sb.ToString() + ParseString(s, i + 1, end);
127     }
128 }

原文地址:https://www.cnblogs.com/liangmou/p/8295858.html

时间: 2024-10-11 06:22:53

Leetcode 394: Decode String的相关文章

Python 解LeetCode:394 Decode String

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

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

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

LeetCode之Decode Ways

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encoded

Leetcode 动态规划 Decode Ways

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Decode Ways Total Accepted: 8689 Total Submissions: 55465 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given a

[LeetCode OJ] Decode Ways

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example,Given encoded

Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给一串包含数字的加密报文,求有多少种解码方式 举个例子,已知报文"12",它可以解码为AB(1 2),也可以是L (12) 所以解码方式有2种. 测试样例 Input: "0" "121212" "1010

【leetcode系列】String to Integer (atoi)

这个我就直接上代码了,最开始把"abc123"也算作合法的了,后来查了一下atoi的定义,把这种去掉了. public class Solution { public static int atoi(String inStr) { long result = 0L; /* * 网上查了一下,atoi函数的定义是如果第一个非空格字符存在,是数字或者正负号则开始做类型转换, * 之后检测到非数字(包括结束符\0)字符时停止转换,返回整型数.否则,返回零.可能的输入情况有: 1.空字符串 *