2
2
3
3
import com .fishercoder .common .classes .Interval ;
4
4
import com .fishercoder .common .classes .ListNode ;
5
-
6
5
import java .util .ArrayList ;
7
6
import java .util .Deque ;
8
7
import java .util .List ;
@@ -13,7 +12,7 @@ public class CommonUtils {
13
12
private static final int DEFAULT_TREE_SIZE = 10 ;
14
13
private static final int DEFAULT_UPPER_BOUND = 100 ;
15
14
16
- //How to make a method generic: declare <T> in its method signature
15
+ // How to make a method generic: declare <T> in its method signature
17
16
public static <T > void printArray_generic_type (T [] nums ) {
18
17
for (T i : nums ) {
19
18
System .out .print (i + ", " );
@@ -22,14 +21,19 @@ public static <T> void printArray_generic_type(T[] nums) {
22
21
}
23
22
24
23
public static void main (String ... strings ) {
25
- Integer [] nums = new Integer []{1 , 2 , 3 , 4 , 5 };
24
+ Integer [] nums = new Integer [] {1 , 2 , 3 , 4 , 5 };
26
25
printArray_generic_type (nums );
27
- String input1 = "[\" zDkA\" ,\" GfAj\" ,\" lt\" ],[\" GfAj\" ,\" rtupD\" ,\" og\" ,\" l\" ],[\" rtupD\" ,\" IT\" ,\" jGcew\" ,\" ZwFqF\" ],[\" og\" ,\" yVobt\" ,\" EjA\" ,\" piUyQ\" ],[\" IT\" ,\" XFlc\" ,\" W\" ,\" rB\" ],[\" l\" ,\" GwQg\" ,\" shco\" ,\" Dub\" ,\" KwgZq\" ],[\" oXMG\" ,\" uqe\" ],[\" sNyV\" ,\" WbrP\" ]" ;
26
+ String input1 =
27
+ "[\" zDkA\" ,\" GfAj\" ,\" lt\" ],[\" GfAj\" ,\" rtupD\" ,\" og\" ,\" l\" ],[\" rtupD\" ,\" IT\" ,\" jGcew\" ,\" ZwFqF\" ],[\" og\" ,\" yVobt\" ,\" EjA\" ,\" piUyQ\" ],[\" IT\" ,\" XFlc\" ,\" W\" ,\" rB\" ],[\" l\" ,\" GwQg\" ,\" shco\" ,\" Dub\" ,\" KwgZq\" ],[\" oXMG\" ,\" uqe\" ],[\" sNyV\" ,\" WbrP\" ]" ;
28
28
String input2 = "[\" A\" ,\" B\" ],[\" C\" ],[\" B\" ,\" C\" ],[\" D\" ]" ;
29
29
CommonUtils .printListList (convertLeetCode2DStringArrayInputIntoJavaArray (input1 ));
30
30
CommonUtils .printListList (convertLeetCode2DStringArrayInputIntoJavaArray (input2 ));
31
- CommonUtils .print (convertLeetCode1DStringArrayInputIntoJavaArray ("[\" abcsi\" ,\" abyzjgj\" ,\" advz\" ,\" ag\" ,\" agkgdkob\" ,\" agpr\" ,\" ail\" ]" ));
32
- CommonUtils .print2DIntArray (convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray ("[448,931,123,345],[889],[214,962],[576,746,897]" ));
31
+ CommonUtils .print (
32
+ convertLeetCode1DStringArrayInputIntoJavaArray (
33
+ "[\" abcsi\" ,\" abyzjgj\" ,\" advz\" ,\" ag\" ,\" agkgdkob\" ,\" agpr\" ,\" ail\" ]" ));
34
+ CommonUtils .print2DIntArray (
35
+ convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray (
36
+ "[448,931,123,345],[889],[214,962],[576,746,897]" ));
33
37
}
34
38
35
39
public static void printArray (boolean [] booleans ) {
@@ -98,8 +102,8 @@ public static List<Integer> randomIntArrayGenerator(int size) {
98
102
99
103
// overloaded method to take no argument
100
104
public static List <Integer > randomIntArrayGenerator () {
101
- return CommonUtils .randomIntArrayGenerator (CommonUtils . DEFAULT_TREE_SIZE ,
102
- DEFAULT_UPPER_BOUND );
105
+ return CommonUtils .randomIntArrayGenerator (
106
+ CommonUtils . DEFAULT_TREE_SIZE , DEFAULT_UPPER_BOUND );
103
107
}
104
108
105
109
// this one has two other overloaded methods as above
@@ -126,7 +130,8 @@ public static List<Integer> uniqueIntArrayGenerator(int size) {
126
130
}
127
131
128
132
// @Notes(context =
129
- // "I'm assuing only classes in this PACKAGE will call the following two methods, so just leave the modifier as default, i.e. no public, private, or protected.")
133
+ // "I'm assuing only classes in this PACKAGE will call the following two methods, so just leave
134
+ // the modifier as default, i.e. no public, private, or protected.")
130
135
public static void printWhitespaces (int count ) {
131
136
for (int i = 0 ; i < count ; i ++) {
132
137
System .out .print (" " );
@@ -228,7 +233,6 @@ public static void printMatrixGeneric(boolean[][] matrix) {
228
233
System .out .println ();
229
234
}
230
235
System .out .println ("----------------------------------------------------" );
231
-
232
236
}
233
237
234
238
public static <T > void printListList (List <List <T >> res ) {
@@ -268,11 +272,13 @@ public static void print2DCharArray(char[][] arrayArrays) {
268
272
}
269
273
270
274
public static char [][] convertLeetCodeRegular2DCharArrayInputIntoJavaArray (String input ) {
271
- /**LeetCode 2-d char array usually comes in like this:
272
- * ["#"," ","#"],[" "," ","#"],["#","c"," "] which is wrapped in double quotes instead of single quotes which makes it not usable in Java code.
273
- * This method helps with the conversion.*/
275
+ /**
276
+ * LeetCode 2-d char array usually comes in like this: ["#"," ","#"],[" ","
277
+ * ","#"],["#","c"," "] which is wrapped in double quotes instead of single quotes which
278
+ * makes it not usable in Java code. This method helps with the conversion.
279
+ */
274
280
String [] arrays = input .split ("],\\ [" );
275
- // CommonUtils.printArray_generic_type(arrays);
281
+ // CommonUtils.printArray_generic_type(arrays);
276
282
int m = arrays .length ;
277
283
int n = arrays [1 ].split ("," ).length ;
278
284
char [][] ans = new char [m ][n ];
@@ -302,13 +308,12 @@ public static char[][] convertLeetCodeRegular2DCharArrayInputIntoJavaArray(Strin
302
308
public static int [][] convertLeetCodeRegularRectangleArrayInputIntoJavaArray (String input ) {
303
309
/**
304
310
* LeetCode 2-d array input usually comes like this: it's a REGULAR rectangle
305
- * [[448,931],[234,889],[214,962],[576,746]]
306
- * The expected input for this method is: "[448,931],[234,889],[214,962],[576,746]"
307
- * i.e. strip off the beginning and ending square brackets, that's it.
308
- * The output of this method will be a standard Java 2-d array.
309
- * */
311
+ * [[448,931],[234,889],[214,962],[576,746]] The expected input for this method is:
312
+ * "[448,931],[234,889],[214,962],[576,746]" i.e. strip off the beginning and ending square
313
+ * brackets, that's it. The output of this method will be a standard Java 2-d array.
314
+ */
310
315
String [] arrays = input .split ("],\\ [" );
311
- // CommonUtils.printArray_generic_type(arrays);
316
+ // CommonUtils.printArray_generic_type(arrays);
312
317
int size = arrays [1 ].split ("," ).length ;
313
318
int [][] output = new int [arrays .length ][size ];
314
319
for (int i = 0 ; i < arrays .length ; i ++) {
@@ -331,18 +336,17 @@ public static int[][] convertLeetCodeRegularRectangleArrayInputIntoJavaArray(Str
331
336
}
332
337
}
333
338
}
334
- // CommonUtils.print2DIntArray(output);
339
+ // CommonUtils.print2DIntArray(output);
335
340
return output ;
336
341
}
337
342
338
343
public static int [][] convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray (String input ) {
339
344
/**
340
345
* LeetCode 2-d array input usually comes like this: each row could have different length
341
- * [[448,931,123,345],[889],[214,962],[576,746,897]]
342
- * The expected input for this method is: "[448,931,123,345],[889],[214,962],[576,746,897]"
343
- * i.e. strip off the beginning and ending square brackets, that's it.
344
- * The output of this method will be a standard Java 2-d array.
345
- * */
346
+ * [[448,931,123,345],[889],[214,962],[576,746,897]] The expected input for this method is:
347
+ * "[448,931,123,345],[889],[214,962],[576,746,897]" i.e. strip off the beginning and ending
348
+ * square brackets, that's it. The output of this method will be a standard Java 2-d array.
349
+ */
346
350
String [] arrays = input .split ("],\\ [" );
347
351
int maxLen = 0 ;
348
352
int [] sizes = new int [arrays .length ];
@@ -389,19 +393,17 @@ public static int[][] convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(S
389
393
390
394
public static List <List <String >> convertLeetCode2DStringArrayInputIntoJavaArray (String input ) {
391
395
/**
392
- * How to copy LeetCode 2-d String array into this method:
393
- * 1. remove the beginning and ending quotes;
394
- * 2. put double quotes into this method parameter;
395
- * 3. copy the input into the double quotes.
396
+ * How to copy LeetCode 2-d String array into this method: 1. remove the beginning and
397
+ * ending quotes; 2. put double quotes into this method parameter; 3. copy the input into
398
+ * the double quotes.
396
399
*
397
- * LeetCode 2-d array input usually comes like this: each row could have different length
398
- * [["A","B"],["C"],["B","C"],["D"]]
399
- * The expected input for this method is: "[\"A\",\"B\"],[\"C\"],[\"B\",\"C\"],[\"D\"]"
400
- * just copy the LeetCode input: ["A","B"],["C"],["B","C"],["D"] into double quotes in Java,
401
- * it'll auto escape the double quotes.
402
- * i.e. strip off the beginning and ending square brackets, that's it.
403
- * The output of this method will be a standard Java 2-d array.
404
- * */
400
+ * <p>LeetCode 2-d array input usually comes like this: each row could have different length
401
+ * [["A","B"],["C"],["B","C"],["D"]] The expected input for this method is:
402
+ * "[\"A\",\"B\"],[\"C\"],[\"B\",\"C\"],[\"D\"]" just copy the LeetCode input:
403
+ * ["A","B"],["C"],["B","C"],["D"] into double quotes in Java, it'll auto escape the double
404
+ * quotes. i.e. strip off the beginning and ending square brackets, that's it. The output of
405
+ * this method will be a standard Java 2-d array.
406
+ */
405
407
String [] arrays = input .split ("],\\ [" );
406
408
List <List <String >> result = new ArrayList <>();
407
409
for (int i = 0 ; i < arrays .length ; i ++) {
@@ -425,12 +427,10 @@ public static List<List<String>> convertLeetCode2DStringArrayInputIntoJavaArray(
425
427
public static List <String > convertLeetCode1DStringArrayInputIntoJavaArray (String input ) {
426
428
/**
427
429
* LeetCode 2-d array input usually comes like this: each row could have different length
428
- * ["A","B","C"]
429
- * The expected input for this method is: "[\"A\",\"B\",\"C\"]"
430
- * just copy the LeetCode input: ["A","B","C"] into double quotes in Java,
431
- * it'll auto escape the double quotes.
432
- * The output of this method will be a standard Java 1-d array.
433
- * */
430
+ * ["A","B","C"] The expected input for this method is: "[\"A\",\"B\",\"C\"]" just copy the
431
+ * LeetCode input: ["A","B","C"] into double quotes in Java, it'll auto escape the double
432
+ * quotes. The output of this method will be a standard Java 1-d array.
433
+ */
434
434
String [] arrays = input .split ("," );
435
435
List <String > result = new ArrayList <>();
436
436
for (int i = 0 ; i < arrays .length ; i ++) {
0 commit comments