给定一个 m x n
二维字符网格 board
和一个字符串单词 word
。如果 word
存在于网格中,返回 true
;否则,返回 false
。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
输入:
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
输出:
true
输入:
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
输出:
true
输入:
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
输出:
false
board
更大的情况下可以更快解决问题?impl Solution {
pub fn exist(board: Vec<Vec<char>>, word: String) -> bool {
fn check(board: &Vec<Vec<char>>, word: &Vec<char>, idx: usize, visited: &mut Vec<Vec<bool>>, r: usize, c: usize) -> bool {
if r >= visited.len() || c >= visited[0].len() || visited[r][c] || board[r][c] != word[idx] {
return false;
}
if idx == word.len() - 1 {
// 完全匹配
return true;
}
visited[r][c] = true;
// 上下左右递归套娃大法
if check(board, word, idx + 1, visited, r - 1, c)
|| check(board, word, idx + 1, visited, r + 1, c)
|| check(board, word, idx + 1, visited, r, c - 1)
|| check(board, word, idx + 1, visited, r, c + 1)
{
return true;
}
visited[r][c] = false;
return false;
}
let word = word.chars().collect::<Vec<_>>();
let mut visited = vec![vec![false; board[0].len()]; board.len()];
for r in 0..board.len() {
for c in 0..board[0].len() {
if check(&board, &word, 0, &mut visited, r, c) {
return true;
}
}
}
return false;
}
}
func exist(board [][]byte, word string) bool {
visited := make([][]bool, len(board))
for i := range visited {
visited[i] = make([]bool, len(board[0]))
}
var check func(idx, r, c int) bool
check = func(idx, r, c int) bool {
if r < 0 || r >= len(board) || c < 0 || c >= len(board[0]) || visited[r][c] || board[r][c] != word[idx] {
return false
}
if idx == len(word)-1 {
// 完全匹配
return true
}
visited[r][c] = true
// 上下左右递归套娃大法
if check(idx+1, r-1, c) || check(idx+1, r+1, c) || check(idx+1, r, c-1) || check(idx+1, r, c+1) {
return true
}
visited[r][c] = false
return false
}
for r, row := range board {
for c := range row {
if check(0, r, c) {
return true
}
}
}
return false
}
class Solution {
private:
bool check(vector<vector<char>>& board, string& word, int idx, vector<vector<bool>>& visited, int r, int c) {
if (r < 0 || r >= visited.size() || c < 0 || c >= visited[0].size() || visited[r][c] || board[r][c] != word[idx]) {
return false;
}
if (idx == word.size() - 1) {
// 完全匹配
return true;
}
visited[r][c] = true;
// 上下左右递归套娃大法
if (check(board, word, idx + 1, visited, r - 1, c)
|| check(board, word, idx + 1, visited, r + 1, c)
|| check(board, word, idx + 1, visited, r, c - 1)
|| check(board, word, idx + 1, visited, r, c + 1)) {
return true;
}
visited[r][c] = false;
return false;
}
public:
bool exist(vector<vector<char>>& board, string word) {
vector<vector<bool>> visited(board.size(), vector<bool>(board[0].size(), false));
for (int r = 0; r < board.size(); ++r) {
for (int c = 0; c < board[0].size(); ++c) {
if (check(board, word, 0, visited, r, c)) {
return true;
}
}
}
return false;
}
};
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
def check(idx: int, r: int, c: int) -> bool:
if r < 0 or r >= len(board) or c < 0 or c >= len(board[0]) or visited[r][c] or board[r][c] != word[idx]:
return False
if idx == len(word) - 1:
return True
visited[r][c] = True
if check(idx + 1, r - 1, c) or check(idx + 1, r + 1, c) or check(idx + 1, r, c - 1) or check(idx + 1, r, c + 1):
return True
visited[r][c] = False
return False
visited = [[False] * len(board[0]) for _ in range(len(board))]
for r in range(len(board)):
for c in range(len(board[0])):
if check(0, r, c):
return True
return False
class Solution {
public boolean exist(char[][] board, String word) {
boolean[][] visited = new boolean[board.length][board[0].length];
for (int r = 0; r < board.length; ++r) {
for (int c = 0; c < board[0].length; ++c) {
if (check(board, word, 0, visited, r, c)) {
return true;
}
}
}
return false;
}
private boolean check(char[][] board, String word, int idx, boolean[][] visited, int r, int c) {
if (r < 0 || r >= visited.length || c < 0 || c >= visited[0].length || visited[r][c] || board[r][c] != word.charAt(idx)) {
return false;
}
if (idx == word.length() - 1) {
// 完全匹配
return true;
}
visited[r][c] = true;
// 上下左右递归套娃大法
if (check(board, word, idx + 1, visited, r - 1, c)
|| check(board, word, idx + 1, visited, r + 1, c)
|| check(board, word, idx + 1, visited, r, c - 1)
|| check(board, word, idx + 1, visited, r, c + 1)) {
return true;
}
visited[r][c] = false;
return false;
}
}
非常感谢你阅读本文~
欢迎【点赞】【收藏】【评论】三连走一波~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://le-yi.blog.csdn.net/ 博客原创~