首页 > 其他分享 >394. Decode String

394. Decode String

时间:2022-12-13 00:33:28浏览次数:47  
标签:String 394 Decode Input sb Stack

394. Decode String 

Example 1:

Input: s = "3[a]2[bc]"
Output: "aaabcbc"

Example 2:

Input: s = "3[a2[c]]"
Output: "accaccacc"

Example 3:

Input: s = "2[abc]3[cd]ef"
Output: "abcabccdcdcdef"

 

class Solution {

   public String decodeString(String s) {        Stack inStack = new Stack<>();        Stack charStack = new Stack<>();       StringBuffer sb = new StringBuffer();        int count = 0;        for(char c : s.toCharArray()) {           int var = Integer.valueOf(c) - '0';           if(var >= 0 && var <= 9)               count = count * 10 + var;           else if(c == '[') {              inStack.add(count);              charStack.add(sb);              count = 0;              sb = new StringBuffer();           }           else if(c == ']') {              StringBuffer tem = sb;              sb = charStack.pop();              for(int k = inStack.pop(); k > 0; k--)                  sb.append(tem);           }          else               sb.append(c);       }        return sb.toString();    } }

标签:String,394,Decode,Input,sb,Stack
From: https://www.cnblogs.com/MarkLeeBYR/p/16977533.html

相关文章