Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit160b749

Browse files
add a method in CommonUtils
1 parent7abdc6e commit160b749

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-
publicstaticint[][]convertLeetCodeArrayInputIntoJavaArray(Stringinput) {
257+
publicstaticint[][]convertLeetCodeRegularRectangleArrayInputIntoJavaArray(Stringinput) {
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
returnoutput;
291291
}
292+
293+
publicstaticint[][]convertLeetCodeIrregularRectangleArrayInputIntoJavaArray(Stringinput) {
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+
intmaxLen =0;
303+
for (inti =0;i <arrays.length;i++) {
304+
String[]strs =arrays[i].split(",");
305+
maxLen =Math.max(maxLen,strs.length);
306+
}
307+
int[][]output =newint[arrays.length][maxLen];
308+
for (inti =0;i <arrays.length;i++) {
309+
if (i ==0) {
310+
Stringstr =arrays[i].substring(1);
311+
String[]nums =str.split(",");
312+
for (intj =0;j <nums.length;j++) {
313+
output[i][j] =Integer.parseInt(nums[j]);
314+
}
315+
}elseif (i ==arrays.length -1) {
316+
Stringstr =arrays[i].substring(0,arrays[i].length() -1);
317+
String[]nums =str.split(",");
318+
for (intj =0;j <nums.length;j++) {
319+
output[i][j] =Integer.parseInt(nums[j]);
320+
}
321+
}else {
322+
String[]nums =arrays[i].split(",");
323+
for (intj =0;j <nums.length;j++) {
324+
output[i][j] =Integer.parseInt(nums[j]);
325+
}
326+
}
327+
}
328+
returnoutput;
329+
}
292330
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp