File tree 3 files changed +76
-0
lines changed
solution/0300-0399/0394.Decode String
3 files changed +76
-0
lines changed Original file line number Diff line number Diff line change @@ -114,6 +114,33 @@ class Solution {
114
114
}
115
115
```
116
116
117
+ ### ** TypeScript**
118
+
119
+ ``` ts
120
+ function decodeString(s : string ): string {
121
+ let ans = ' ' ;
122
+ let stack = [];
123
+ let count = 0 ; // repeatCount
124
+ for (let cur of s ) {
125
+ if ((/ [0-9 ] / .test (cur ))) {
126
+ count = count * 10 + Number (cur );
127
+ } else if (/ [a-z ] / .test (cur )) {
128
+ ans += cur ;
129
+ } else if (' [' == cur ) {
130
+ stack .push ([ans , count ]);
131
+ // reset
132
+ ans = ' ' ;
133
+ count = 0 ;
134
+ } else {
135
+ // match ']'
136
+ let [pre, count] = stack .pop ();
137
+ ans = pre + ans .repeat (count );
138
+ }
139
+ }
140
+ return ans ;
141
+ };
142
+ ```
143
+
117
144
### ** ...**
118
145
119
146
```
Original file line number Diff line number Diff line change @@ -94,6 +94,33 @@ class Solution {
94
94
}
95
95
```
96
96
97
+ ### ** TypeScript**
98
+
99
+ ``` ts
100
+ function decodeString(s : string ): string {
101
+ let ans = ' ' ;
102
+ let stack = [];
103
+ let count = 0 ; // repeatCount
104
+ for (let cur of s ) {
105
+ if ((/ [0-9 ] / .test (cur ))) {
106
+ count = count * 10 + Number (cur );
107
+ } else if (/ [a-z ] / .test (cur )) {
108
+ ans += cur ;
109
+ } else if (' [' == cur ) {
110
+ stack .push ([ans , count ]);
111
+ // reset
112
+ ans = ' ' ;
113
+ count = 0 ;
114
+ } else {
115
+ // match ']'
116
+ let [pre, count] = stack .pop ();
117
+ ans = pre + ans .repeat (count );
118
+ }
119
+ }
120
+ return ans ;
121
+ };
122
+ ```
123
+
97
124
### ** ...**
98
125
99
126
```
Original file line number Diff line number Diff line change
1
+ function decodeString ( s : string ) : string {
2
+ let ans = '' ;
3
+ let stack = [ ] ;
4
+ let count = 0 ; // repeatCount
5
+ for ( let cur of s ) {
6
+ if ( ( / [ 0 - 9 ] / . test ( cur ) ) ) {
7
+ count = count * 10 + Number ( cur ) ;
8
+ } else if ( / [ a - z ] / . test ( cur ) ) {
9
+ ans += cur ;
10
+ } else if ( '[' == cur ) {
11
+ stack . push ( [ ans , count ] ) ;
12
+ // reset
13
+ ans = '' ;
14
+ count = 0 ;
15
+ } else {
16
+ // match ']'
17
+ let [ pre , count ] = stack . pop ( ) ;
18
+ ans = pre + ans . repeat ( count ) ;
19
+ }
20
+ }
21
+ return ans ;
22
+ } ;
You can’t perform that action at this time.
0 commit comments