-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0394-DecodeString.cs
49 lines (44 loc) · 1.38 KB
/
0394-DecodeString.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//-----------------------------------------------------------------------------
// Runtime: 80ms
// Memory Usage:
// Link: https://leetcode.com/submissions/detail/379046685/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Text;
namespace LeetCode
{
public class _0394_DecodeString
{
public string DecodeString(string s)
{
var countStack = new Stack<int>();
var strStack = new Stack<string>();
var sb = new StringBuilder();
var num = 0;
foreach (var ch in s)
{
if (char.IsDigit(ch))
num = num * 10 + ch - '0';
else if (ch == '[')
{
strStack.Push(sb.ToString());
countStack.Push(num);
sb.Clear();
num = 0;
}
else if (ch == ']')
{
var str = sb.ToString();
sb.Clear();
sb.Append(strStack.Pop());
var count = countStack.Pop();
for (int i = 0; i < count; i++)
sb.Append(str);
}
else
sb.Append(ch);
}
return sb.ToString();
}
}
}