LeetCode79 Word Search

本文解析了LeetCode 79 WordSearch题目,介绍了一种通过递归回溯的方法来查找给定单词是否能在二维字符网格中找到。文章详细解释了如何通过保存二维数组的状态来标记已经使用的单元格,并提供了完整的Java代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

For example,
Given board =

[
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
]

word = “ABCCED”, -> returns true,

word = “SEE”, -> returns true,

word = “ABCB”, -> returns false.

问题分析

这道题我感觉就是简单的考验对数组的操作,因为还有一个状态的问题,所以,需要保存整个二维数组的状态来代表这个单元格的元素有没有被使用。

其中需要多考虑到的一点是避免出现越界的情况。

代码如下

public boolean exist(char[][] board, String word) {
    if(board==null||board.length<1||board[0].length<1){
        return false;
    }
    int m = board.length;
    int n = board[0].length;
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[0].length; j++) {
            if(board[i][j]==word.charAt(0)){
                boolean[][] dp = new boolean[m][n];
                dp[i][j]=true;
                help(board,word,dp,1,i,j);
                if (result) {
                    return result;
                }
            }
        }
    }
    return  result;
}
boolean result  =false;
private void  help(char[][] board,String word,boolean[][] dp,int index,int i,int j){
    if(index==word.length()){
        result=true;
    }else {
        if(result==true){
            return;
        }
        if(i<board.length-1&&board[i+1][j]==word.charAt(index)&& !dp[i+1][j]){
            dp[i+1][j]=true;
            help(board,word,dp,index+1,i+1,j);
            dp[i+1][j]=false;
        }
        if(j<board[0].length-1&&board[i][j+1]==word.charAt(index)&& !dp[i][j+1]){
            dp[i][j+1]=true;
            help(board,word,dp,index+1,i,j+1);
            dp[i][j+1]=false;
        }
        if(i>0&&board[i-1][j]==word.charAt(index)&& !dp[i-1][j]){
            dp[i-1][j]=true;
            help(board,word,dp,index+1,i-1,j);
            dp[i-1][j]=false;
        }
        if(j>0&&board[i][j-1]==word.charAt(index)&&!dp[i][j-1]){
            dp[i][j-1]=true;
            help(board,word,dp,index+1,i,j-1);
            dp[i][j-1]=false;
        }
    }
}

LeetCode学习笔记持续更新

GitHub地址 https://github.com/yanqinghe/leetcode

CSDN博客地址 http://blog.csdn.net/yanqinghe123/article/category/7176678

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值