Skip to content

Commit 21f7db2

Browse files
add another method in CommonUtils
1 parent 160b749 commit 21f7db2

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
public static void main(String... strings) {
2525
Integer[] nums = new Integer[]{1, 2, 3, 4, 5};
2626
printArray_generic_type(nums);
27+
CommonUtils.printListList(convertLeetCodeStringArrayInputIntoJavaArray("[\"A\",\"B\"],[\"C\"],[\"B\",\"C\"],[\"D\"]"));
2728
}
2829

2930
public static void printArray(boolean[] booleans) {
@@ -327,4 +328,34 @@ public static int[][] convertLeetCodeIrregularRectangleArrayInputIntoJavaArray(S
327328
}
328329
return output;
329330
}
331+
332+
public static List<List<String>> convertLeetCodeStringArrayInputIntoJavaArray(String input) {
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 = new ArrayList<>();
344+
for (int i = 0; i < arrays.length; i++) {
345+
List<String> level = new ArrayList<>();
346+
String[] strings;
347+
if (i == 0) {
348+
strings = arrays[i].substring(1).split(",");
349+
} else if (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 (int j = 0; j < strings.length; j++) {
355+
level.add(strings[j]);
356+
}
357+
result.add(level);
358+
}
359+
return result;
360+
}
330361
}

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