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

Commit21f7db2

Browse files
add another method in CommonUtils
1 parent160b749 commit21f7db2

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static <T> void printArray_generic_type(T[] nums) {
2424
publicstaticvoidmain(String...strings) {
2525
Integer[]nums =newInteger[]{1,2,3,4,5};
2626
printArray_generic_type(nums);
27+
CommonUtils.printListList(convertLeetCodeStringArrayInputIntoJavaArray("[\"A\",\"B\"],[\"C\"],[\"B\",\"C\"],[\"D\"]"));
2728
}
2829

2930
publicstaticvoidprintArray(boolean[]booleans) {
@@ -327,4 +328,34 @@ public static int[][] convertLeetCodeIrregularRectangleArrayInputIntoJavaArray(S
327328
}
328329
returnoutput;
329330
}
331+
332+
publicstaticList<List<String>>convertLeetCodeStringArrayInputIntoJavaArray(Stringinput) {
333+
/**
334+
* LeetCode 2-d array input usually comes like this: each row could have different length
335+
* [["A","B"],["C"],["B","C"],["D"]]
336+
* The expected input for this method is: "[\"A\",\"B\"],[\"C\"],[\"B\",\"C\"],[\"D\"]"
337+
* just copy the LeetCode input: ["A","B"],["C"],["B","C"],["D"] into double quotes in Java,
338+
* it'll auto escape the double quotes.
339+
* i.e. strip off the beginning and ending square brackets, that's it.
340+
* The output of this method will be a standard Java 2-d array.
341+
* */
342+
String[]arrays =input.split("],\\[");
343+
List<List<String>>result =newArrayList<>();
344+
for (inti =0;i <arrays.length;i++) {
345+
List<String>level =newArrayList<>();
346+
String[]strings;
347+
if (i ==0) {
348+
strings =arrays[i].substring(1).split(",");
349+
}elseif (i ==arrays.length -1) {
350+
strings =arrays[i].substring(0,arrays[i].length() -1).split(",");
351+
}else {
352+
strings =arrays[i].split(",");
353+
}
354+
for (intj =0;j <strings.length;j++) {
355+
level.add(strings[j]);
356+
}
357+
result.add(level);
358+
}
359+
returnresult;
360+
}
330361
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp