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

Commit4fa86d2

Browse files
add 2018
1 parent9ff09b3 commit4fa86d2

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|2018|[Check if Word Can Be Placed In Crossword](https://leetcode.com/problems/check-if-word-can-be-placed-in-crossword/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2018.java)||Medium||
1112
|2012|[Sum of Beauty in the Array](https://leetcode.com/problems/sum-of-beauty-in-the-array/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2012.java)||Medium||
1213
|2011|[Final Value of Variable After Performing Operations](https://leetcode.com/problems/final-value-of-variable-after-performing-operations/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2011.java)||Easy||
1314
|2007|[Find Original Array From Doubled Array](https://leetcode.com/problems/find-original-array-from-doubled-array/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2007.java)||Medium||
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
packagecom.fishercoder.solutions;
2+
3+
publicclass_2018 {
4+
publicstaticclassSolution1 {
5+
publicbooleanplaceWordInCrossword(char[][]board,Stringword) {
6+
intm =board.length;
7+
intn =board[0].length;
8+
for (inti =0;i <m;i++) {
9+
for (intj =0;j <n;j++) {
10+
if (board[i][j] ==' ' ||board[i][j] ==word.charAt(0)) {
11+
if (canPlaceTopDown(word,board,i,j) ||canPlaceLeftRight(word,board,i,j)
12+
||canPlaceBottomUp(word,board,i,j) ||canPlaceRightLeft(word,board,i,j)) {
13+
returntrue;
14+
}
15+
}
16+
}
17+
}
18+
returnfalse;
19+
}
20+
21+
privatebooleancanPlaceRightLeft(Stringword,char[][]board,introw,intcol) {
22+
if (col +1 <board[0].length && (Character.isLowerCase(board[row][col +1]) ||board[row][col +1] ==' ')) {
23+
returnfalse;
24+
}
25+
intk =0;
26+
intj =col;
27+
for (;j >=0 &&k <word.length();j--) {
28+
if (board[row][j] !=word.charAt(k) &&board[row][j] !=' ') {
29+
returnfalse;
30+
}else {
31+
k++;
32+
}
33+
}
34+
returnk ==word.length() && (j <0 ||board[row][j] =='#');
35+
}
36+
37+
privatebooleancanPlaceBottomUp(Stringword,char[][]board,introw,intcol) {
38+
if (row +1 <board.length && (Character.isLowerCase(board[row +1][col]) ||board[row +1][col] ==' ')) {
39+
returnfalse;
40+
}
41+
intk =0;
42+
inti =row;
43+
for (;i >=0 &&k <word.length();i--) {
44+
if (board[i][col] !=word.charAt(k) &&board[i][col] !=' ') {
45+
returnfalse;
46+
}else {
47+
k++;
48+
}
49+
}
50+
returnk ==word.length() && (i <0 ||board[i][col] =='#');
51+
}
52+
53+
privatebooleancanPlaceLeftRight(Stringword,char[][]board,introw,intcol) {
54+
if (col >0 && (Character.isLowerCase(board[row][col -1]) ||board[row][col -1] ==' ')) {
55+
returnfalse;
56+
}
57+
intk =0;
58+
intj =col;
59+
for (;j <board[0].length &&k <word.length();j++) {
60+
if (board[row][j] !=word.charAt(k) &&board[row][j] !=' ') {
61+
returnfalse;
62+
}else {
63+
k++;
64+
}
65+
}
66+
returnk ==word.length() && (j ==board[0].length ||board[row][j] =='#');
67+
}
68+
69+
privatebooleancanPlaceTopDown(Stringword,char[][]board,introw,intcol) {
70+
if (row >0 && (Character.isLowerCase(board[row -1][col]) ||board[row -1][col] ==' ')) {
71+
returnfalse;
72+
}
73+
intk =0;
74+
inti =row;
75+
for (;i <board.length &&k <word.length();i++) {
76+
if (board[i][col] !=word.charAt(k) &&board[i][col] !=' ') {
77+
returnfalse;
78+
}else {
79+
k++;
80+
}
81+
}
82+
returnk ==word.length() && (i ==board.length ||board[i][col] =='#');
83+
}
84+
}
85+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._2018;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_2018Test {
10+
privatestatic_2018.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_2018.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(true,solution1.placeWordInCrossword(newchar[][]{
20+
{'#',' ','#'},
21+
{' ',' ','#'},
22+
{'#','c',' '}
23+
},"abc"));
24+
}
25+
26+
@Test
27+
publicvoidtest2() {
28+
assertEquals(false,solution1.placeWordInCrossword(newchar[][]{
29+
{' ','#','a'},
30+
{' ','#','c'},
31+
{' ','#','a'}
32+
},"ac"));
33+
}
34+
35+
@Test
36+
publicvoidtest3() {
37+
assertEquals(true,solution1.placeWordInCrossword(newchar[][]{
38+
{'#',' ','#'},
39+
{' ',' ','#'},
40+
{'#',' ','c'}
41+
},"ca"));
42+
}
43+
44+
@Test
45+
publicvoidtest4() {
46+
assertEquals(true,solution1.placeWordInCrossword(newchar[][]{
47+
{'#',' ','#'},
48+
{' ',' ','#'},
49+
{'#',' ','c'}
50+
},"cd"));
51+
}
52+
53+
@Test
54+
publicvoidtest5() {
55+
assertEquals(true,solution1.placeWordInCrossword(newchar[][]{
56+
{'#',' ','#'},
57+
{' ','#','#'},
58+
{'#',' ','c'}
59+
},"ca"));
60+
}
61+
62+
@Test
63+
publicvoidtest6() {
64+
assertEquals(true,solution1.placeWordInCrossword(newchar[][]{
65+
{'#',' ','#'},
66+
{'#','c','#'},
67+
{'#','#','c'}
68+
},"ca"));
69+
}
70+
71+
@Test
72+
publicvoidtest7() {
73+
assertEquals(false,solution1.placeWordInCrossword(newchar[][]{
74+
{' ','b','#'},
75+
{' ','#','#'},
76+
{' ',' ','c'}
77+
},"ca"));
78+
}
79+
80+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp