力扣79. 单词搜索(java DFS解法)

Problem: 79. 单词搜索

题目描述

在这里插入图片描述在这里插入图片描述在这里插入图片描述

思路

该问题可以归纳为一类遍历二维矩阵的题目,此类中的一部分题目可以利用DFS来解决,具体到本题目(该题目可以的写法大体不变可参看前面几个题目:):

Problem: 力扣面试题 08.10. 颜色填充(java DFS解法)
Problem: 力扣200. 岛屿数量(java DFS解法)
Problem: 力扣LCR 130. 衣橱整理(DFS 解法)

具体到本题目中:

我们要从矩阵中的每一个字母开始并判断是是否继续DFS,在本题目中,我们针对每一个当前遍历的字符均要定义一个和给定二维数组一样大的boolean类型的辅助数组visited;同时我们要注意在递归结束后,我们还要将当前visited位置的状态恢复,因为我们在DFS查找到某一个字符时,可能该字符和其前面的字符还是满足给定带查找字符串的一部分。我们也该注意到,题目只需要我们判断是否存在给定的字符串即可,即当我们找到一个时就可提前退出!!!

解题方法

1.定义记录提前退出的boolea类型变量existed,给定矩阵的行、列(并初始化)
2.遍历给定矩阵,每一个位置的字符都要定义一个给定二维数组一样大的boolean类型的辅助数组visited;调用dfs函数,判断existed,判断是否提前退出
3.dfs函数实现:

3.1 如果当前existed为true则直接返回,若当前给定字符串中的第k个字符和当前遍历给定矩阵board得到的字符不匹配则直接返回;
3.2将当前visited合法位置标记为true;
3.3如果递归深度已经等于字符串得最后一个字符,则表示存在一个和给定字符串匹配的路径,则返回
3.4 记录当前遍历位置的四个方位int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
3.5for循环(范围1~4),循环中每次执行** int newI = i + directions[k][0];int newJ = j + directions[k][1];**用于记录当前位置的新的位置,并判断当前新位置是否合法,若合法则DFS递归调用(在新的位置的基础上)
3.6 恢复当前visited为false

复杂度

时间复杂度:

O ( M N ⋅ 4 L ) O(MN \cdot 4^L) O(MN4L),L为指定字符串的长度,M、N分别为visited矩阵的长度与宽度

空间复杂度:

O ( M N ) O(MN) O(MN)

Code

class Solution {
    //Define an early exit variable
    private boolean existed = false;
    //Given the rows and columns of the matrix
    private int rows;
    private int cols;

    /**
     * Determines whether the given string exists in the given matrix
     *
     * @param board Given matrix
     * @param word  Given string
     * @return boolean
     */
    public boolean exist(char[][] board, String word) {
        rows = board.length;
        cols = board[0].length;
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                boolean[][] visited = new boolean[rows][cols];
                dfs(board, word, i, j, 0, visited);
                if (existed) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Use DFS to find a given string in a given matrix
     *
     * @param board   Given matrix
     * @param word    Given string
     * @param i       Abscissa
     * @param j       Ordinate
     * @param k       Given the KTH character in the string
     * @param visited Auxiliary array
     */
    private void dfs(char[][] board, String word, int i, int j, int k, boolean[][] visited) {
        //Early exit condition
        if (existed == true) {
            return;
        }
        //If there are unequal characters, exit directly
        if (word.charAt(k) != board[i][j]) {
            return;
        }
        visited[i][j] = true;
        //Have found
        if (k == word.length() - 1) {
            existed = true;
            return;
        }
        int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        for (int di = 0; di < 4; ++di) {
            int newI = i + directions[di][0];
            int newJ = j + directions[di][1];
            if (newI >= 0 && newI < rows && newJ >= 0 && newJ < cols
                    && !visited[newI][newJ]) {
                dfs(board, word, newI, newJ, k + 1, visited);
            }
        }
        //Restore the current position of visited
        visited[i][j] = false;
    }
}
  • 20
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值