@@ -254,9 +254,9 @@ public static void print2DCharArray(char[][] arrayArrays) {
254
254
System .out .println ();
255
255
}
256
256
257
- public static int [][] convertLeetCodeArrayInputIntoJavaArray (String input ) {
257
+ public static int [][] convertLeetCodeRegularRectangleArrayInputIntoJavaArray (String input ) {
258
258
/**
259
- * LeetCode 2-d array input usually comes like this:
259
+ * LeetCode 2-d array input usually comes like this: it's a REGULAR rectangle
260
260
* [[448,931],[234,889],[214,962],[576,746]]
261
261
* The expected input for this method is: "[448,931],[234,889],[214,962],[576,746]"
262
262
* i.e. strip off the beginning and ending square brackets, that's it.
@@ -289,4 +289,42 @@ public static int[][] convertLeetCodeArrayInputIntoJavaArray(String input) {
289
289
// CommonUtils.print2DIntArray(output);
290
290
return output ;
291
291
}
292
+
293
+ public static int [][] convertLeetCodeIrregularRectangleArrayInputIntoJavaArray (String input ) {
294
+ /**
295
+ * LeetCode 2-d array input usually comes like this: each row could have different length
296
+ * [[448,931,123,345],[889],[214,962],[576,746,897]]
297
+ * The expected input for this method is: "[448,931,123,345],[889],[214,962],[576,746,897]"
298
+ * i.e. strip off the beginning and ending square brackets, that's it.
299
+ * The output of this method will be a standard Java 2-d array.
300
+ * */
301
+ String [] arrays = input .split ("],\\ [" );
302
+ int maxLen = 0 ;
303
+ for (int i = 0 ; i < arrays .length ; i ++) {
304
+ String [] strs = arrays [i ].split ("," );
305
+ maxLen = Math .max (maxLen , strs .length );
306
+ }
307
+ int [][] output = new int [arrays .length ][maxLen ];
308
+ for (int i = 0 ; i < arrays .length ; i ++) {
309
+ if (i == 0 ) {
310
+ String str = arrays [i ].substring (1 );
311
+ String [] nums = str .split ("," );
312
+ for (int j = 0 ; j < nums .length ; j ++) {
313
+ output [i ][j ] = Integer .parseInt (nums [j ]);
314
+ }
315
+ } else if (i == arrays .length - 1 ) {
316
+ String str = arrays [i ].substring (0 , arrays [i ].length () - 1 );
317
+ String [] nums = str .split ("," );
318
+ for (int j = 0 ; j < nums .length ; j ++) {
319
+ output [i ][j ] = Integer .parseInt (nums [j ]);
320
+ }
321
+ } else {
322
+ String [] nums = arrays [i ].split ("," );
323
+ for (int j = 0 ; j < nums .length ; j ++) {
324
+ output [i ][j ] = Integer .parseInt (nums [j ]);
325
+ }
326
+ }
327
+ }
328
+ return output ;
329
+ }
292
330
}
0 commit comments