@@ -26,6 +26,7 @@ public static void main(String... strings) {
2626 printArray_generic_type (nums );
2727 CommonUtils .printListList (convertLeetCode2DStringArrayInputIntoJavaArray ("[\" A\" ,\" B\" ],[\" C\" ],[\" B\" ,\" C\" ],[\" D\" ]" ));
2828 CommonUtils .print (convertLeetCode1DStringArrayInputIntoJavaArray ("[\" abcsi\" ,\" abyzjgj\" ,\" advz\" ,\" ag\" ,\" agkgdkob\" ,\" agpr\" ,\" ail\" ]" ));
29+ CommonUtils .print2DIntArray (convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray ("[448,931,123,345],[889],[214,962],[576,746,897]" ));
2930 }
3031
3132 public static void printArray (boolean [] booleans ) {
@@ -292,7 +293,7 @@ public static int[][] convertLeetCodeRegularRectangleArrayInputIntoJavaArray(Str
292293 return output ;
293294 }
294295
295- public static int [][] convertLeetCodeIrregularRectangleArrayInputIntoJavaArray (String input ) {
296+ public static int [][] convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray (String input ) {
296297 /**
297298 * LeetCode 2-d array input usually comes like this: each row could have different length
298299 * [[448,931,123,345],[889],[214,962],[576,746,897]]
@@ -302,27 +303,32 @@ public static int[][] convertLeetCodeIrregularRectangleArrayInputIntoJavaArray(S
302303 * */
303304 String [] arrays = input .split ("],\\ [" );
304305 int maxLen = 0 ;
306+ int [] sizes = new int [arrays .length ];
305307 for (int i = 0 ; i < arrays .length ; i ++) {
306308 String [] strs = arrays [i ].split ("," );
307309 maxLen = Math .max (maxLen , strs .length );
310+ sizes [i ] = strs .length ;
308311 }
309- int [][] output = new int [arrays .length ][maxLen ];
312+ int [][] output = new int [arrays .length ][];
310313 for (int i = 0 ; i < arrays .length ; i ++) {
311314 if (i == 0 ) {
312315 String str = arrays [i ].substring (1 );
313316 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 ++) {
315319 output [i ][j ] = Integer .parseInt (nums [j ]);
316320 }
317321 } else if (i == arrays .length - 1 ) {
318322 String str = arrays [i ].substring (0 , arrays [i ].length () - 1 );
319323 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 ++) {
321326 output [i ][j ] = Integer .parseInt (nums [j ]);
322327 }
323328 } else {
324329 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 ++) {
326332 output [i ][j ] = Integer .parseInt (nums [j ]);
327333 }
328334 }
0 commit comments