Skip to content

Commit 7fe4422

Browse files
add another method in CommonUtils
1 parent 21f7db2 commit 7fe4422

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ 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\"]"));
27+
CommonUtils.printListList(convertLeetCode2DStringArrayInputIntoJavaArray("[\"A\",\"B\"],[\"C\"],[\"B\",\"C\"],[\"D\"]"));
28+
CommonUtils.print(convertLeetCode1DStringArrayInputIntoJavaArray("[\"abcsi\",\"abyzjgj\",\"advz\",\"ag\",\"agkgdkob\",\"agpr\",\"ail\"]"));
2829
}
2930

3031
public static void printArray(boolean[] booleans) {
@@ -329,7 +330,7 @@ public static int[][] convertLeetCodeIrregularRectangleArrayInputIntoJavaArray(S
329330
return output;
330331
}
331332

332-
public static List<List<String>> convertLeetCodeStringArrayInputIntoJavaArray(String input) {
333+
public static List<List<String>> convertLeetCode2DStringArrayInputIntoJavaArray(String input) {
333334
/**
334335
* LeetCode 2-d array input usually comes like this: each row could have different length
335336
* [["A","B"],["C"],["B","C"],["D"]]
@@ -358,4 +359,29 @@ public static List<List<String>> convertLeetCodeStringArrayInputIntoJavaArray(St
358359
}
359360
return result;
360361
}
362+
363+
public static List<String> convertLeetCode1DStringArrayInputIntoJavaArray(String input) {
364+
/**
365+
* LeetCode 2-d array input usually comes like this: each row could have different length
366+
* ["A","B","C"]
367+
* The expected input for this method is: "[\"A\",\"B\",\"C\"]"
368+
* just copy the LeetCode input: ["A","B","C"] into double quotes in Java,
369+
* it'll auto escape the double quotes.
370+
* The output of this method will be a standard Java 1-d array.
371+
* */
372+
String[] arrays = input.split(",");
373+
List<String> result = new ArrayList<>();
374+
for (int i = 0; i < arrays.length; i++) {
375+
String word;
376+
if (i == 0) {
377+
word = arrays[i].substring(1);
378+
} else if (i == arrays.length - 1) {
379+
word = arrays[i].substring(0, arrays[i].length() - 1);
380+
} else {
381+
word = arrays[i];
382+
}
383+
result.add(word);
384+
}
385+
return result;
386+
}
361387
}

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