using System; using System.Collections.Generic; using System.Text; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串 */ public string decodeString (string s) { // write code here // write code here if (string.IsNullOrWhiteSpace(s)) return s; Stack<char> cStack = new Stack<char>(); foreach (char c in s) { if (c == ']') { StringBuilder sb = new StringBuilder(); while (cStack.Count != 0 && cStack.Peek() != '[') sb.Insert(0, cStack.Pop()); cStack.Pop(); StringBuilder sbN = new StringBuilder(); while (cStack.Count != 0 && cStack.Peek() >= '0' && cStack.Peek() <= '9') sbN.Insert(0, cStack.Pop()); //cStack.Pop(); for (int n = 0; n < int.Parse(sbN.ToString()); n++) { for (int i = 0; i < sb.Length; i++) cStack.Push(sb[i]); } } else cStack.Push(c); } StringBuilder sbR = new StringBuilder(); while (cStack.Count != 0) sbR.Insert(0, cStack.Pop()); return sbR.ToString(); } }