Title Description: According to the provisions, the string decoding, specific examples see the topic link
- Idea: Use two stacks to store numbers and letters, respectively
- Note 1: Numbers are multi-bit words, to be processed after entering the digital stack
- Note 2: The combined string generated during the stack will continue into the letter stack
- Note 3: Remember that when the alphabet is out of the stack, the characters are combined into strings.
Note 4: The reason for using the letter stack without strings is that the join efficiency of the string is higher than the string addition
Results: 98.02% MS, beat
Cons: Judging is the number there is a bit of code is not concise, you can move J to the outside of the loop
classSolution (Object):defDecodestring ( Self, s):""": Type S:str: Rtype:str """Nums, chars=[], [] I, length= 0,Len(s) whileI<Length:j=I+ 1 ifS[i].isdigit (): num= int(S[i]) whileJ<LengthifS[j].isdigit (): num=Num* Ten + int(S[j]) J+= 1 Else: BreakNums.append (num)elifS[i]== ' [' orS[i].isalpha (): Chars.append (S[i])Else: T, TMPC=Chars.pop (), [] whileT!= ' [': Tmpc.append (t) t=Chars.pop () TCHARs=Nums.pop ()* "'. Join (tmpc[::-1]) Chars.append (tchars) I=Jreturn "'. Join (Chars)
Python Solution leetcode:394 Decode String