|
1 | 1 | packagecom_github_leetcode; |
2 | 2 |
|
3 | | -importjava.util.ArrayList; |
4 | | -importjava.util.Collections; |
5 | 3 | importjava.util.List; |
6 | 4 |
|
7 | 5 | publicclassCommonUtils { |
@@ -54,40 +52,6 @@ public static boolean compareMatrix(List<List<Integer>> mt1, List<List<Integer>> |
54 | 52 | returntrue; |
55 | 53 | } |
56 | 54 |
|
57 | | -publicstaticchar[][]convertLeetCodeRegular2DCharArrayInputIntoJavaArray(Stringinput) { |
58 | | -/* |
59 | | - * LeetCode 2-d char array usually comes in like this: |
60 | | - * ["#"," ","#"],[" "," ","#"],["#","c"," "] which is wrapped in double quotes instead |
61 | | - * of single quotes which makes it not usable in Java code. |
62 | | - * This method helps with the conversion. |
63 | | - */ |
64 | | -String[]arrays =input.split("],\\["); |
65 | | -intm =arrays.length; |
66 | | -intn =arrays[1].split(",").length; |
67 | | -char[][]ans =newchar[m][n]; |
68 | | -for (inti =0;i <m;i++) { |
69 | | -if (i ==0) { |
70 | | -Stringstr =arrays[i].substring(1); |
71 | | -String[]strs =str.split(","); |
72 | | -for (intj =0;j <strs.length;j++) { |
73 | | -ans[i][j] =strs[j].charAt(1); |
74 | | - } |
75 | | - }elseif (i ==m -1) { |
76 | | -Stringstr =arrays[i].substring(0,arrays[i].length() -1); |
77 | | -String[]strs =str.split(","); |
78 | | -for (intj =0;j <strs.length;j++) { |
79 | | -ans[i][j] =strs[j].charAt(1); |
80 | | - } |
81 | | - }else { |
82 | | -String[]strs =arrays[i].split(","); |
83 | | -for (intj =0;j <strs.length;j++) { |
84 | | -ans[i][j] =strs[j].charAt(1); |
85 | | - } |
86 | | - } |
87 | | - } |
88 | | -returnans; |
89 | | - } |
90 | | - |
91 | 55 | publicstaticint[][]convertLeetCodeRegularRectangleArrayInputIntoJavaArray(Stringinput) { |
92 | 56 | /* |
93 | 57 | * LeetCode 2-d array input usually comes like this: it's a REGULAR rectangle |
@@ -173,62 +137,4 @@ public static int[][] convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(S |
173 | 137 | } |
174 | 138 | returnoutput; |
175 | 139 | } |
176 | | - |
177 | | -publicstaticList<List<String>>convertLeetCode2DStringArrayInputIntoJavaArray(Stringinput) { |
178 | | -/* |
179 | | - * How to copy LeetCode 2-d String array into this method: |
180 | | - * 1. remove the beginning and ending quotes; |
181 | | - * 2. put double quotes into this method parameter; |
182 | | - * 3. copy the input into the double quotes. |
183 | | - * |
184 | | - * LeetCode 2-d array input usually comes like this: each row could have different length |
185 | | - * [["A","B"],["C"],["B","C"],["D"]] |
186 | | - * The expected input for this method is: "[\"A\",\"B\"],[\"C\"],[\"B\",\"C\"],[\"D\"]" |
187 | | - * just copy the LeetCode input: ["A","B"],["C"],["B","C"],["D"] into double quotes in Java, |
188 | | - * it'll auto escape the double quotes. |
189 | | - * i.e. strip off the beginning and ending square brackets, that's it. |
190 | | - * The output of this method will be a standard Java 2-d array. |
191 | | - * */ |
192 | | -String[]arrays =input.split("],\\["); |
193 | | -List<List<String>>result =newArrayList<>(); |
194 | | -for (inti =0;i <arrays.length;i++) { |
195 | | -List<String>level =newArrayList<>(); |
196 | | -String[]strings; |
197 | | -if (i ==0) { |
198 | | -strings =arrays[i].substring(1).split(","); |
199 | | - }elseif (i ==arrays.length -1) { |
200 | | -strings =arrays[i].substring(0,arrays[i].length() -1).split(","); |
201 | | - }else { |
202 | | -strings =arrays[i].split(","); |
203 | | - } |
204 | | -Collections.addAll(level,strings); |
205 | | -result.add(level); |
206 | | - } |
207 | | -returnresult; |
208 | | - } |
209 | | - |
210 | | -publicstaticList<String>convertLeetCode1DStringArrayInputIntoJavaArray(Stringinput) { |
211 | | -/* |
212 | | - * LeetCode 2-d array input usually comes like this: each row could have different length |
213 | | - * ["A","B","C"] |
214 | | - * The expected input for this method is: "[\"A\",\"B\",\"C\"]" |
215 | | - * just copy the LeetCode input: ["A","B","C"] into double quotes in Java, |
216 | | - * it'll auto escape the double quotes. |
217 | | - * The output of this method will be a standard Java 1-d array. |
218 | | - * */ |
219 | | -String[]arrays =input.split(","); |
220 | | -List<String>result =newArrayList<>(); |
221 | | -for (inti =0;i <arrays.length;i++) { |
222 | | -Stringword; |
223 | | -if (i ==0) { |
224 | | -word =arrays[i].substring(1); |
225 | | - }elseif (i ==arrays.length -1) { |
226 | | -word =arrays[i].substring(0,arrays[i].length() -1); |
227 | | - }else { |
228 | | -word =arrays[i]; |
229 | | - } |
230 | | -result.add(word); |
231 | | - } |
232 | | -returnresult; |
233 | | - } |
234 | 140 | } |