@@ -26,6 +26,7 @@ public static void main(String... strings) {
26
26
printArray_generic_type (nums );
27
27
CommonUtils .printListList (convertLeetCode2DStringArrayInputIntoJavaArray ("[\" A\" ,\" B\" ],[\" C\" ],[\" B\" ,\" C\" ],[\" D\" ]" ));
28
28
CommonUtils .print (convertLeetCode1DStringArrayInputIntoJavaArray ("[\" abcsi\" ,\" abyzjgj\" ,\" advz\" ,\" ag\" ,\" agkgdkob\" ,\" agpr\" ,\" ail\" ]" ));
29
+ CommonUtils .print2DIntArray (convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray ("[448,931,123,345],[889],[214,962],[576,746,897]" ));
29
30
}
30
31
31
32
public static void printArray (boolean [] booleans ) {
@@ -292,7 +293,7 @@ public static int[][] convertLeetCodeRegularRectangleArrayInputIntoJavaArray(Str
292
293
return output ;
293
294
}
294
295
295
- public static int [][] convertLeetCodeIrregularRectangleArrayInputIntoJavaArray (String input ) {
296
+ public static int [][] convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray (String input ) {
296
297
/**
297
298
* LeetCode 2-d array input usually comes like this: each row could have different length
298
299
* [[448,931,123,345],[889],[214,962],[576,746,897]]
@@ -302,27 +303,32 @@ public static int[][] convertLeetCodeIrregularRectangleArrayInputIntoJavaArray(S
302
303
* */
303
304
String [] arrays = input .split ("],\\ [" );
304
305
int maxLen = 0 ;
306
+ int [] sizes = new int [arrays .length ];
305
307
for (int i = 0 ; i < arrays .length ; i ++) {
306
308
String [] strs = arrays [i ].split ("," );
307
309
maxLen = Math .max (maxLen , strs .length );
310
+ sizes [i ] = strs .length ;
308
311
}
309
- int [][] output = new int [arrays .length ][maxLen ];
312
+ int [][] output = new int [arrays .length ][];
310
313
for (int i = 0 ; i < arrays .length ; i ++) {
311
314
if (i == 0 ) {
312
315
String str = arrays [i ].substring (1 );
313
316
String [] nums = str .split ("," );
314
- for (int j = 0 ; j < nums .length ; j ++) {
317
+ output [i ] = new int [sizes [i ]];
318
+ for (int j = 0 ; j < sizes [i ]; j ++) {
315
319
output [i ][j ] = Integer .parseInt (nums [j ]);
316
320
}
317
321
} else if (i == arrays .length - 1 ) {
318
322
String str = arrays [i ].substring (0 , arrays [i ].length () - 1 );
319
323
String [] nums = str .split ("," );
320
- for (int j = 0 ; j < nums .length ; j ++) {
324
+ output [i ] = new int [sizes [i ]];
325
+ for (int j = 0 ; j < sizes [i ]; j ++) {
321
326
output [i ][j ] = Integer .parseInt (nums [j ]);
322
327
}
323
328
} else {
324
329
String [] nums = arrays [i ].split ("," );
325
- for (int j = 0 ; j < nums .length ; j ++) {
330
+ output [i ] = new int [sizes [i ]];
331
+ for (int j = 0 ; j < sizes [i ]; j ++) {
326
332
output [i ][j ] = Integer .parseInt (nums [j ]);
327
333
}
328
334
}
0 commit comments