@@ -144,40 +144,40 @@ var exist = function(board, word) {
144
144
145
145
``` go
146
146
func exist (board [][]byte , word string ) bool {
147
- if len (board) == 0 {
148
- return false
149
- }
150
- // 标记数组
151
- isVisited := make ([][]bool ,len (board))
152
- for i := 0 ; i < len (board); i++ {
153
- isVisited[i] = make ([]bool , len (board[0 ]))
154
- }
155
- for i := 0 ; i < len (board); i++ {
156
- for j := 0 ; j < len (board[0 ]); j++ {
157
- if board[i][j] == word[0 ] {
158
- if bfs (board,i,j, isVisited,word,0 ) {
159
- return true
160
- }
161
- }
162
- }
163
- }
164
- return false
147
+ if len (board) == 0 {
148
+ return false
149
+ }
150
+ // 标记数组
151
+ isVisited := make ([][]bool , len (board))
152
+ for i := 0 ; i < len (board); i++ {
153
+ isVisited[i] = make ([]bool , len (board[0 ]))
154
+ }
155
+ for i := 0 ; i < len (board); i++ {
156
+ for j := 0 ; j < len (board[0 ]); j++ {
157
+ if board[i][j] == word[0 ] {
158
+ if bfs (board, i, j, isVisited, word, 0 ) {
159
+ return true
160
+ }
161
+ }
162
+ }
163
+ }
164
+ return false
165
165
}
166
166
167
- func bfs (board [][]byte , i ,j int , isVisited [][]bool , word string , index int ) bool {
168
- if index == len (word) {
169
- return true
170
- }
171
- if i < 0 || j < 0 || i == len (board) || j == len (board[0 ]) || isVisited[i][j] || board[i][j] != word[index] {
172
- return false
173
- }
174
- isVisited[i][j] = true
175
- res := bfs (board, i+1 , j, isVisited, word, index+1 ) ||
176
- bfs (board, i, j+1 , isVisited, word, index+1 ) ||
177
- bfs (board, i-1 , j, isVisited, word, index+1 ) ||
178
- bfs (board, i, j-1 , isVisited, word, index+1 )
179
- isVisited[i][j] = false
180
- return res
167
+ func bfs (board [][]byte , i , j int , isVisited [][]bool , word string , index int ) bool {
168
+ if index == len (word) {
169
+ return true
170
+ }
171
+ if i < 0 || j < 0 || i == len (board) || j == len (board[0 ]) || isVisited[i][j] || board[i][j] != word[index] {
172
+ return false
173
+ }
174
+ isVisited[i][j] = true
175
+ res := bfs (board, i+1 , j, isVisited, word, index+1 ) ||
176
+ bfs (board, i, j+1 , isVisited, word, index+1 ) ||
177
+ bfs (board, i-1 , j, isVisited, word, index+1 ) ||
178
+ bfs (board, i, j-1 , isVisited, word, index+1 )
179
+ isVisited[i][j] = false
180
+ return res
181
181
}
182
182
```
183
183
0 commit comments