@@ -24,7 +24,8 @@ 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
+ CommonUtils .printListList (convertLeetCode2DStringArrayInputIntoJavaArray ("[\" A\" ,\" B\" ],[\" C\" ],[\" B\" ,\" C\" ],[\" D\" ]" ));
28
+ CommonUtils .print (convertLeetCode1DStringArrayInputIntoJavaArray ("[\" abcsi\" ,\" abyzjgj\" ,\" advz\" ,\" ag\" ,\" agkgdkob\" ,\" agpr\" ,\" ail\" ]" ));
28
29
}
29
30
30
31
public static void printArray (boolean [] booleans ) {
@@ -329,7 +330,7 @@ public static int[][] convertLeetCodeIrregularRectangleArrayInputIntoJavaArray(S
329
330
return output ;
330
331
}
331
332
332
- public static List <List <String >> convertLeetCodeStringArrayInputIntoJavaArray (String input ) {
333
+ public static List <List <String >> convertLeetCode2DStringArrayInputIntoJavaArray (String input ) {
333
334
/**
334
335
* LeetCode 2-d array input usually comes like this: each row could have different length
335
336
* [["A","B"],["C"],["B","C"],["D"]]
@@ -358,4 +359,29 @@ public static List<List<String>> convertLeetCodeStringArrayInputIntoJavaArray(St
358
359
}
359
360
return result ;
360
361
}
362
+
363
+ public static List <String > convertLeetCode1DStringArrayInputIntoJavaArray (String input ) {
364
+ /**
365
+ * LeetCode 2-d array input usually comes like this: each row could have different length
366
+ * ["A","B","C"]
367
+ * The expected input for this method is: "[\"A\",\"B\",\"C\"]"
368
+ * just copy the LeetCode input: ["A","B","C"] into double quotes in Java,
369
+ * it'll auto escape the double quotes.
370
+ * The output of this method will be a standard Java 1-d array.
371
+ * */
372
+ String [] arrays = input .split ("," );
373
+ List <String > result = new ArrayList <>();
374
+ for (int i = 0 ; i < arrays .length ; i ++) {
375
+ String word ;
376
+ if (i == 0 ) {
377
+ word = arrays [i ].substring (1 );
378
+ } else if (i == arrays .length - 1 ) {
379
+ word = arrays [i ].substring (0 , arrays [i ].length () - 1 );
380
+ } else {
381
+ word = arrays [i ];
382
+ }
383
+ result .add (word );
384
+ }
385
+ return result ;
386
+ }
361
387
}
0 commit comments