【leetcode hot 100 79】单词搜索

解法一:(回溯法)建立一个二维数组,标识该位置的数是否已经遍历,0为未遍历,1为已遍历;每次回溯函数都在已经找到的数的四周找word中的下一个字母,回溯标志为used[i][j]=0

java 复制代码
class Solution {
    // 建立两个数组,以便遍历相邻的单元格
    int[] row = new int[]{0,1,0,-1};
    int[] col = new int[]{1,0,-1,0};

    public boolean exist(char[][] board, String word) {
        // 建立一个二维数组,标识该位置的数是否已经遍历,0为未遍历,1为已遍历
        int[][] used = new int[board.length][board[0].length];
        boolean result = false;
        for(int i=0; i<board.length; i++){
            for(int j=0; j<board[0].length; j++){
                if(board[i][j]==word.charAt(0)){
                    used[i][j]=1;
                    result = backtrack(board, word, used, i, j, 1); // 1表示开始找word的第1个数
                    used[i][j]=0; // 记得回溯
                    if(result==true){
                        return true;
                    }
                }
            }
        }
        return result;
    }

    public boolean backtrack(char[][] board, String word, int[][] used, int i, int j, int num){
        if(num >= word.length()){
            // 已经找完了
            return true;
        }
        boolean result = false;
        for(int n=0;n<4;n++){
            int x = i + row[n];
            int y = j + col[n];
            if(x>=0 && x<board.length && y>=0 && y<board[0].length && used[x][y]==0 && board[x][y]==word.charAt(num)){
                used[x][y]=1;
                result = backtrack(board, word, used, x, y, num+1); 
                if(result==true){
                    return true;
                }
                used[x][y]=0; // 回溯
            }
        }
        return result;
    }
}

注意:

  • 在非回溯函数exist()中,也要记得回溯:used[i][j]=0
  • 遍历相邻元素时,不可以j += idy,这样会导致j变化跳过几个数不比较;要int x = i + row[n]
  • 不能双for:for(int idx:row){for(int idy:col){...}},这样会导致判断一些不是相邻元素;要for(int n=0;n<4;n++)并取row[n]col[n]
相关推荐
闻缺陷则喜何志丹20 分钟前
【二分查找 树状数组 差分数组 离散化 】P6172 [USACO16FEB] Load Balancing P|省选-
c++·算法·二分查找·洛谷·离散化·差分数组·数组数组
肖筱小瀟44 分钟前
2025-3-23 leetcode刷题情况(动态规划)
算法·leetcode·动态规划
Dreams_l1 小时前
排序算法(插入,希尔,选择,冒泡,堆,快排,归并)
数据结构·算法·排序算法
秋凉 づᐇ1 小时前
数据结构--红黑树
数据结构·c++·算法
张子栋1 小时前
单调栈总结
算法
不爱学英文的码字机器1 小时前
[操作系统] 进程间通信:进程池的实现
java·开发语言·算法
GalaxyPokemon1 小时前
LINUX基础IO [七] - 文件缓冲区的深入理解
linux·运维·算法
lwewan1 小时前
26考研——图_图的存储(6)
数据结构·笔记·考研·算法·深度优先
qystca2 小时前
备份比赛数据【算法赛】
算法·模拟·二分
羽殇之舞2 小时前
分布式唯一 ID 生成算法笔记
后端·算法