File tree 1 file changed +50
-0
lines changed
1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ package leetcode ;
2
+
3
+ import java .util .Stack ;
4
+
5
+ public class DecodeString {
6
+
7
+ public static void main (String [] args ) {
8
+ DecodeString test = new DecodeString ();
9
+ System .out .println (test .decodeString ("3[a2[c]]" ));
10
+
11
+ }
12
+
13
+ public String decodeString (String s ) {
14
+ Stack <strNode > stack = new Stack <>();
15
+ stack .push (new strNode (1 ));
16
+ int n =0 ;
17
+ for (char c : s .toCharArray ()){
18
+ if (Character .isDigit (c )){
19
+ int v = Integer .parseInt ("" +c );
20
+ n = n *10 +v ;
21
+ }
22
+ if (c =='[' ){
23
+ stack .push (new strNode (n ));
24
+ n =0 ;
25
+ }
26
+ if (Character .isAlphabetic (c )){
27
+ stack .peek ().str .append (c );
28
+ }
29
+ if (c ==']' ){
30
+ int times = stack .peek ().number ;
31
+ String str = stack .pop ().str .toString ();
32
+ while (times >0 ){
33
+ stack .peek ().str .append (str );
34
+ times --;
35
+ }
36
+ }
37
+ }
38
+ return stack .pop ().str .toString ();
39
+ }
40
+
41
+ class strNode {
42
+ int number ;
43
+ StringBuilder str ;
44
+ public strNode (int n ){
45
+ number = n ;
46
+ str = new StringBuilder ();
47
+ }
48
+ }
49
+ }
50
+
You can’t perform that action at this time.
0 commit comments