Commit c40929f 1 parent 631791e commit c40929f Copy full SHA for c40929f
File tree 4 files changed +85
-0
lines changed
solution/0900-0999/0921.Minimum Add to Make Parentheses Valid
4 files changed +85
-0
lines changed Original file line number Diff line number Diff line change @@ -286,4 +286,39 @@ function minAddToMakeValid(s: string): number {
286
286
287
287
<!-- solution: end -->
288
288
289
+ <!-- solution: start -->
290
+
291
+ ### 方法三:替换 + 递归
292
+
293
+ <!-- tabs: start -->
294
+
295
+ #### TypeScript
296
+
297
+ ``` ts
298
+ function minAddToMakeValid(s : string ): number {
299
+ const l = s .length ;
300
+ s = s .replace (' ()' , ' ' );
301
+
302
+ return s .length === l ? l : minAddToMakeValid (s );
303
+ }
304
+ ```
305
+
306
+ #### JavaScript
307
+
308
+ ``` js
309
+ /**
310
+ * @param {string} s
311
+ * @return {number}
312
+ */
313
+ var minAddToMakeValid = function (s ) {
314
+ const l = s .length ;
315
+ s = s .replace (' ()' , ' ' );
316
+ return s .length === l ? l : minAddToMakeValid (s);
317
+ };
318
+ ```
319
+
320
+ <!-- tabs: end -->
321
+
322
+ <!-- solution: end -->
323
+
289
324
<!-- problem: end -->
Original file line number Diff line number Diff line change @@ -284,4 +284,39 @@ function minAddToMakeValid(s: string): number {
284
284
285
285
<!-- solution: end -->
286
286
287
+ <!-- solution: start -->
288
+
289
+ ### Solution 3: Replace + recursion
290
+
291
+ <!-- tabs: start -->
292
+
293
+ #### TypeScript
294
+
295
+ ``` ts
296
+ function minAddToMakeValid(s : string ): number {
297
+ const l = s .length ;
298
+ s = s .replace (' ()' , ' ' );
299
+
300
+ return s .length === l ? l : minAddToMakeValid (s );
301
+ }
302
+ ```
303
+
304
+ #### JavaScript
305
+
306
+ ``` js
307
+ /**
308
+ * @param {string} s
309
+ * @return {number}
310
+ */
311
+ var minAddToMakeValid = function (s ) {
312
+ const l = s .length ;
313
+ s = s .replace (' ()' , ' ' );
314
+ return s .length === l ? l : minAddToMakeValid (s);
315
+ };
316
+ ```
317
+
318
+ <!-- tabs: end -->
319
+
320
+ <!-- solution: end -->
321
+
287
322
<!-- problem: end -->
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {number }
4
+ */
5
+ var minAddToMakeValid = function ( s ) {
6
+ const l = s . length ;
7
+ s = s . replace ( '()' , '' ) ;
8
+ return s . length === l ? l : minAddToMakeValid ( s ) ;
9
+ } ;
Original file line number Diff line number Diff line change
1
+ function minAddToMakeValid ( s : string ) : number {
2
+ const l = s . length ;
3
+ s = s . replace ( '()' , '' ) ;
4
+
5
+ return s . length === l ? l : minAddToMakeValid ( s ) ;
6
+ }
You can’t perform that action at this time.
0 commit comments