1 Star 0 Fork 0

Gv0YuH4n9/leetcode-golang

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
nowcoder_JZ_65.go 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
Gv0YuH4n9 提交于 2022-03-02 00:00 +08:00 . feat
package main
func hasPath(matrix [][]byte, word string) bool {
// write code here
row := len(matrix)
col := len(matrix[0])
strLen := len(word)
if row == 0 || col == 0 || strLen == 0 {
return false
}
isVisited := make([][]bool, row)
for k := 0; k < row; k++ {
isVisited[k] = make([]bool, col)
}
for i := 0; i < row; i++ {
for j := 0; j < col; j++ {
if matrix[i][j] == word[0] {
if dfs(row, col, strLen, i, j, 0, word, matrix, isVisited) {
return true
}
}
}
}
return false
}
func dfs(row int, col int, strLen int, i int, j int, count int, word string, matrix [][]byte, isVisited [][]bool) bool {
if i < 0 || j < 0 || i >= row || j >= col || isVisited[i][j] || matrix[i][j] != word[count] {
return false
}
if count == strLen-1 {
return true
}
isVisited[i][j] = true
if dfs(row, col, strLen, i-1, j, count+1, word, matrix, isVisited) ||
dfs(row, col, strLen, i+1, j, count+1, word, matrix, isVisited) ||
dfs(row, col, strLen, i, j-1, count+1, word, matrix, isVisited) ||
dfs(row, col, strLen, i, j+1, count+1, word, matrix, isVisited) {
return true
} else {
isVisited[i][j] = false
return false
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JAVA-GC/leetcode-golang.git
git@gitee.com:JAVA-GC/leetcode-golang.git
JAVA-GC
leetcode-golang
leetcode-golang
master

搜索帮助