给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
题目链接:https://leetcode.cn/problems/ju-zhen-zhong-de-lu-jing-lcof/
自己想的递归方法,想节省空间用字符串记录访问路径,后来发现还不如用数组,不过也算自己独立完成的,笨是笨了些,好在大体思路正确。
时间:2354 ms,内存:41.9 MB。
class Solution {
public boolean exist(char[][] board, String word) {
int row = board.length;
int col = board[0].length;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (checkWord(board, word, 0, "", i, j) == true) return true;
}
}
return false;
}
public boolean checkWord (char[][] board, String word, int charIndex, String path, int rowIndex, int colIndex) {
if (charIndex == word.length()) return true;
if (rowIndex < 0 || rowIndex >= board.length || colIndex < 0 || colIndex >= board[0].length) return false;
if (path.indexOf(""+rowIndex+colIndex) > -1) return false;
if (board[rowIndex][colIndex] == word.charAt(charIndex)) {
path = path + rowIndex + colIndex + " ";
return checkWord(board, word, charIndex+1, path, rowIndex-1, colIndex)
|| checkWord(board, word, charIndex+1, path, rowIndex+1, colIndex)
|| checkWord(board, word, charIndex+1, path, rowIndex, colIndex-1)
|| checkWord(board, word, charIndex+1, path, rowIndex, colIndex+1);
} else {
return false;
}
}
}
参考官方思路修改了代码,重点是使用访问数组记录已经访问的元素,遍历失败时回溯到上个节点寻找其他路径,并恢复访问数组元素。
时间:168 ms,内存:41.7 MB。
class Solution {
public boolean exist(char[][] board, String word) {
int row = board.length, col = board[0].length;
boolean[][] visited = new boolean[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (checkWord(board, word, visited, i, j, 0)) return true;
}
}
return false;
}
public boolean checkWord (char[][] board, String word, boolean[][] visited, int rowIndex, int colIndex, int wordIndex) {
if (board[rowIndex][colIndex] != word.charAt(wordIndex)){
return false;
} else if (wordIndex == word.length()-1) {
return true;
}
visited[rowIndex][colIndex] = true;
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (int[] dir : directions) {
int newRow = rowIndex + dir[0];
int newCol = colIndex + dir[1];
if (newRow >= 0 && newRow < board.length && newCol >= 0 && newCol < board[0].length) {
if (!visited[newRow][newCol]) {
if (checkWord(board, word, visited, newRow, newCol, wordIndex+1)) return true;
}
}
}
visited[rowIndex][colIndex] = false;
return false;
}
}
除了开辟新数组用于存放访问元素,也可以直接在原数组上进行修改,将访问过的元素改为空白字符,不过这种方法感觉不是很规范。
时间:166 ms,内存:41.7 MB。
class Solution {
public boolean exist(char[][] board, String word) {
int row = board.length, col = board[0].length;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (checkWord(board, word, i, j, 0)) return true;
}
}
return false;
}
public boolean checkWord (char[][] board, String word, int rowIndex, int colIndex, int wordIndex) {
if (board[rowIndex][colIndex] != word.charAt(wordIndex)){
return false;
} else if (wordIndex == word.length()-1) {
return true;
}
board[rowIndex][colIndex] = ' ';
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (int[] dir : directions) {
int newRow = rowIndex + dir[0];
int newCol = colIndex + dir[1];
if (newRow >= 0 && newRow < board.length && newCol >= 0 && newCol < board[0].length) {
if (checkWord(board, word, newRow, newCol, wordIndex+1)) return true;
}
}
board[rowIndex][colIndex] = word.charAt(wordIndex);
return false;
}
}
其实思路大多数是一致的,代码细节上不一样,结果区别也挺大。
时间:87 ms,内存:39.5 MB。
class Solution {
public boolean exist(char[][] board, String word) {
int row = board.length, col = board[0].length;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (checkWord(board, word, i, j, 0)) return true;
}
}
return false;
}
public boolean checkWord (char[][] board, String word, int rowIndex, int colIndex, int wordIndex) {
if (wordIndex == word.length()) return true;
if (rowIndex < 0 || rowIndex == board.length || colIndex < 0 || colIndex == board[0].length || board[rowIndex][colIndex] != word.charAt(wordIndex)) return false;
board[rowIndex][colIndex] = ' ';
boolean res = checkWord(board, word, rowIndex+1, colIndex, wordIndex+1)
|| checkWord(board, word, rowIndex-1, colIndex, wordIndex+1)
|| checkWord(board, word, rowIndex, colIndex+1, wordIndex+1)
|| checkWord(board, word, rowIndex, colIndex-1, wordIndex+1);
board[rowIndex][colIndex] = word.charAt(wordIndex);
return res;
}
}