filmov
tv
Leetcode 394 decode string

Показать описание
time complex : O(n)
Space complex: O(n)
solution :
two stacks one is saved StringBuilder another is for number
two-variable: one is to StringBuilder as the result, another is saved the number
loop the string char by char
there are 4 possibles :
if char is a number, change the number value
if char is a letter, appended it to StringBuilder
if char is left square bracket, add number and StringBuilder to their stack respectively, and rest their value as initial
the char is a right square bracket, create a new temporary StringBuilder and pop the StringBuilder and number from this stack
Space complex: O(n)
solution :
two stacks one is saved StringBuilder another is for number
two-variable: one is to StringBuilder as the result, another is saved the number
loop the string char by char
there are 4 possibles :
if char is a number, change the number value
if char is a letter, appended it to StringBuilder
if char is left square bracket, add number and StringBuilder to their stack respectively, and rest their value as initial
the char is a right square bracket, create a new temporary StringBuilder and pop the StringBuilder and number from this stack