LeetCode第79题 - 单词搜索

题目

解答

java 复制代码
class Solution {
    public boolean exist(char[][] board, String word) {
        int rowLength = board.length;
        int colomnLength = board[0].length;

        boolean[][] boardUsed = new boolean[rowLength][colomnLength];

        for (int i = 0; i < rowLength; ++i) {
            Arrays.fill(boardUsed[i], false);
        }

        char firstChar = word.charAt(0);
        for (int row = 0; row < rowLength; ++row) {
            for (int colomn = 0; colomn < colomnLength; ++colomn) {
                if (board[row][colomn] == firstChar) {
                    for (int i = 0; i < rowLength; ++i) {
                        Arrays.fill(boardUsed[i], false);
                    }

                    boardUsed[row][colomn] = true;
                    if (exist(board, word, 1, boardUsed, row, colomn)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public boolean exist(char[][] board, String word, int index, boolean[][] boardUsed, int rowIndex, int colomnIndex) {

        if (index >= word.length()) {
            return true;
        }

        int rowLength = board.length;
        int colomnLength = board[0].length;

        int nextIndex = index + 1;
        char c = word.charAt(index);
        boardUsed[rowIndex][colomnIndex] = true;
        int nextRow = 0;
        int nextColomn = 0;
        // 上一行
        {
            nextRow = rowIndex - 1;
            nextColomn = colomnIndex;
            if (nextRow >= 0 && !boardUsed[nextRow][nextColomn] && board[nextRow][nextColomn] == c) {
                if (exist(board, word, nextIndex, boardUsed, nextRow, nextColomn)) {
                    return true;
                } else {
                    boardUsed[nextRow][nextColomn] = false;
                }
            }
        }
        // 下一行
        {
            nextRow = rowIndex + 1;
            nextColomn = colomnIndex;

            if (nextRow < rowLength && !boardUsed[nextRow][nextColomn] && board[nextRow][nextColomn] == c) {

                if (exist(board, word, nextIndex, boardUsed, nextRow, nextColomn)) {
                    return true;
                } else {
                    boardUsed[nextRow][nextColomn] = false;
                }
            }
        }
        // 左边
        {
            nextRow = rowIndex;
            nextColomn = colomnIndex - 1;
            if (nextColomn >= 0 && !boardUsed[nextRow][nextColomn] && board[nextRow][nextColomn] == c) {
                if (exist(board, word, nextIndex, boardUsed, nextRow, nextColomn)) {
                    return true;
                } else {
                    boardUsed[nextRow][nextColomn] = false;
                }
            }
        }
        // 右边
        {
            nextRow = rowIndex;
            nextColomn = colomnIndex + 1;

            if (nextColomn < colomnLength && !boardUsed[nextRow][nextColomn] && board[nextRow][nextColomn] == c) {
                if (exist(board, word, nextIndex, boardUsed, nextRow, nextColomn)) {
                    return true;
                } else {
                    boardUsed[nextRow][nextColomn] = false;
                }
            }
        }

        return false;
    }
}

总结

回溯法。

相关推荐
倒头就睡的小比特1 天前
算法竞赛C++常用的STL
c++·算法
小羊没烦恼!1 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼1 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
旖旎夜光2 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark2 天前
大规模并行计算中的负载均衡算法研究4
算法
Because_of_Her12 天前
并查集-听课笔记
笔记·算法·并查集
码流子2 天前
高速公路安全监测实践:碰撞监测预警+物联网底座,从感知到处置的闭环
大数据·人工智能·物联网·算法·架构
another heaven2 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
wzdark2 天前
从算法设计模式看编程思维的抽象能力4
算法
2601_962218612 天前
万象生鲜系统称重自动多退少补算法解决生鲜非标品痛点
大数据·数据库·人工智能·python·算法