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

Commite65f15f

Browse files
authored
added solution for 212 (#154)
1 parent865429d commite65f15f

File tree

1 file changed

+40
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+40
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_212.java

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

33
importjava.util.ArrayList;
4+
importjava.util.HashSet;
45
importjava.util.List;
56

67
publicclass_212 {
@@ -68,4 +69,43 @@ private TrieNode buildTrie(String[] words) {
6869
returnroot;
6970
}
7071
}
72+
73+
publicstaticclassSolution2 {
74+
publicList<String>findWords (char[][]board,String[]words) {
75+
76+
List<String>result =newArrayList();
77+
HashSet<String>set =newHashSet();
78+
for (Stringword :words) {
79+
for (inti =0;i <board.length;i++) {
80+
for (intj =0;j <board[0].length;j++) {
81+
if (board[i][j] ==word.charAt(0) &&search(board,i,j,0,word)) {
82+
set.add(word);
83+
}
84+
}
85+
}
86+
}
87+
result =newArrayList<>(set);
88+
returnresult;
89+
}
90+
91+
privatebooleansearch(char[][]board,inti,intj,intcount,Stringword) {
92+
if (count ==word.length()) {
93+
returntrue;
94+
}
95+
96+
if (i <0 ||i >=board.length ||j <0 ||j >=board[0].length ||board[i][j] !=word.charAt(count)) {
97+
returnfalse;
98+
}
99+
100+
chartemp =board[i][j];
101+
board[i][j] =' ';
102+
103+
booleanfoundWord =search(board,i +1,j,count +1,word)
104+
||search(board,i -1,j,count +1,word)
105+
||search(board,i,j +1,count +1,word)
106+
||search(board,i,j -1,count +1,word);
107+
board[i][j] =temp;
108+
returnfoundWord;
109+
}
110+
}
71111
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp