Skip to content

Commit 160b749

Browse files
add a method in CommonUtils
1 parent 7abdc6e commit 160b749

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/main/java/com/fishercoder/common/utils/CommonUtils.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ public static void print2DCharArray(char[][] arrayArrays) {
254254
System.out.println();
255255
}
256256

257-
public static int[][] convertLeetCodeArrayInputIntoJavaArray(String input) {
257+
public static int[][] convertLeetCodeRegularRectangleArrayInputIntoJavaArray(String input) {
258258
/**
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
260260
* [[448,931],[234,889],[214,962],[576,746]]
261261
* The expected input for this method is: "[448,931],[234,889],[214,962],[576,746]"
262262
* i.e. strip off the beginning and ending square brackets, that's it.
@@ -289,4 +289,42 @@ public static int[][] convertLeetCodeArrayInputIntoJavaArray(String input) {
289289
// CommonUtils.print2DIntArray(output);
290290
return output;
291291
}
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+
}
292330
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy