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

Commit4626e39

Browse files
add a method to convert char array
1 parentb45ab1d commit4626e39

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,40 @@ public static void print2DCharArray(char[][] arrayArrays) {
264264
System.out.println();
265265
}
266266

267+
publicstaticchar[][]convertLeetCodeRegular2DCharArrayInputIntoJavaArray(Stringinput) {
268+
/**LeetCode 2-d char array usually comes in like this:
269+
* [["#"," ","#"],[" "," ","#"],["#","c"," "]] which is wrapped in double quotes instead of single quotes which makes it not usable in Java code.
270+
* This method helps with the conversion.*/
271+
input =input.substring(1,input.length() -1);
272+
String[]arrays =input.split("],\\[");
273+
// CommonUtils.printArray_generic_type(arrays);
274+
intm =arrays.length;
275+
intn =arrays[1].split(",").length;
276+
char[][]ans =newchar[m][n];
277+
for (inti =0;i <m;i++) {
278+
if (i ==0) {
279+
Stringstr =arrays[i].substring(1);
280+
String[]strs =str.split(",");
281+
for (intj =0;j <strs.length;j++) {
282+
ans[i][j] =strs[j].charAt(1);
283+
}
284+
}elseif (i ==m -1) {
285+
Stringstr =arrays[i].substring(0,arrays[i].length() -1);
286+
String[]strs =str.split(",");
287+
for (intj =0;j <strs.length;j++) {
288+
ans[i][j] =strs[j].charAt(1);
289+
}
290+
}else {
291+
String[]strs =arrays[i].split(",");
292+
for (intj =0;j <strs.length;j++) {
293+
ans[i][j] =strs[j].charAt(1);
294+
}
295+
}
296+
}
297+
CommonUtils.print2DCharArray(ans);
298+
returnans;
299+
}
300+
267301
publicstaticint[][]convertLeetCodeRegularRectangleArrayInputIntoJavaArray(Stringinput) {
268302
/**
269303
* LeetCode 2-d array input usually comes like this: it's a REGULAR rectangle

‎src/test/java/com/fishercoder/_2018Test.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
packagecom.fishercoder;
22

3+
importcom.fishercoder.common.utils.CommonUtils;
34
importcom.fishercoder.solutions._2018;
45
importorg.junit.BeforeClass;
56
importorg.junit.Test;
@@ -77,4 +78,11 @@ public void test7() {
7778
},"ca"));
7879
}
7980

81+
@Test
82+
publicvoidtest8() {
83+
assertEquals(true,solution1.placeWordInCrossword(
84+
CommonUtils.convertLeetCodeRegular2DCharArrayInputIntoJavaArray("[[\"#\",\"\",\"#\"],[\"\",\"\",\"#\"],[\"#\",\"c\",\"\"]]"),
85+
"abc"));
86+
}
87+
8088
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp