@@ -24,6 +24,7 @@ public static <T> void printArray_generic_type(T[] nums) {
24
24
public static void main (String ... strings ) {
25
25
Integer [] nums = new Integer []{1 , 2 , 3 , 4 , 5 };
26
26
printArray_generic_type (nums );
27
+ CommonUtils .printListList (convertLeetCodeStringArrayInputIntoJavaArray ("[\" A\" ,\" B\" ],[\" C\" ],[\" B\" ,\" C\" ],[\" D\" ]" ));
27
28
}
28
29
29
30
public static void printArray (boolean [] booleans ) {
@@ -327,4 +328,34 @@ public static int[][] convertLeetCodeIrregularRectangleArrayInputIntoJavaArray(S
327
328
}
328
329
return output ;
329
330
}
331
+
332
+ public static List <List <String >> convertLeetCodeStringArrayInputIntoJavaArray (String input ) {
333
+ /**
334
+ * LeetCode 2-d array input usually comes like this: each row could have different length
335
+ * [["A","B"],["C"],["B","C"],["D"]]
336
+ * The expected input for this method is: "[\"A\",\"B\"],[\"C\"],[\"B\",\"C\"],[\"D\"]"
337
+ * just copy the LeetCode input: ["A","B"],["C"],["B","C"],["D"] into double quotes in Java,
338
+ * it'll auto escape the double quotes.
339
+ * i.e. strip off the beginning and ending square brackets, that's it.
340
+ * The output of this method will be a standard Java 2-d array.
341
+ * */
342
+ String [] arrays = input .split ("],\\ [" );
343
+ List <List <String >> result = new ArrayList <>();
344
+ for (int i = 0 ; i < arrays .length ; i ++) {
345
+ List <String > level = new ArrayList <>();
346
+ String [] strings ;
347
+ if (i == 0 ) {
348
+ strings = arrays [i ].substring (1 ).split ("," );
349
+ } else if (i == arrays .length - 1 ) {
350
+ strings = arrays [i ].substring (0 , arrays [i ].length () - 1 ).split ("," );
351
+ } else {
352
+ strings = arrays [i ].split ("," );
353
+ }
354
+ for (int j = 0 ; j < strings .length ; j ++) {
355
+ level .add (strings [j ]);
356
+ }
357
+ result .add (level );
358
+ }
359
+ return result ;
360
+ }
330
361
}
0 commit comments