212. 单词搜索 II

复制代码
https://leetcode.cn/problems/word-search-ii/description/?envType=study-plan-v2&envId=top-interview-150

思路:

  1. 我们可以先将这个单词网格转化成一颗字典树,然后再拿words进行搜索。

  2. 我们可以先将words装换成字典树,然后在在board中选点看能不能走出一条满足的路径来

对于这两个思路其实差不多,但是1的话如果board太大就很容易超时,而且无用的构建会很多(比如有一个5*5的board,理论一条路径长度应该是25,但是如果words中长的单词都只有5的话我们就根本用不到这么长)。

所以权衡来看第二种思路会更好

思路一(确实会超时):

java 复制代码
class Trie {
        Trie[] children;
        boolean isEnd;
        public Trie() {
            children = new Trie[26];
            isEnd = true; // 每个被构造出的节点都是一个单词的结尾
        }
    }
    public List<String> findWords(char[][] board, String[] words) {
        boolean[][] signed = new boolean[board.length][board[0].length];
        Trie root = new Trie();
        for(int i = 0; i < board.length; i++) {
            for(int j = 0; j < board[0].length; j++) {
                signed[i][j] = true;
                if(root.children[board[i][j] - 'a'] == null) {
                    root.children[board[i][j] - 'a'] = new Trie();
                }
                transfer(board, signed, root.children[board[i][j] - 'a'], i, j);
                signed[i][j] = false;
            }
        }
        List<String> ans = new ArrayList<>();
        for(String word : words) {
            if(search(root, word)) {
                ans.add(word);
            }
        }
        return ans;
    }
    public void transfer(char[][] board, boolean[][] signed, Trie root, int x, int y) {
        for(int i = 0; i < 4; i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if(xx >= 0 && xx < board.length && yy >= 0 && yy < board[0].length && !signed[xx][yy]) {
                signed[xx][yy] = true;
                if(root.children[board[xx][yy] - 'a'] == null) root.children[board[xx][yy] - 'a'] = new Trie();
                transfer(board, signed, root.children[board[xx][yy] - 'a'], xx, yy);
                signed[xx][yy] = false;
            }
        }
    }

思路二:

java 复制代码
    class Trie {
        Trie[] children;
        String word; // 如果当前节点是不是一个单词的结尾,那么word=这个单词,否则word=""
        public Trie() {
            children = new Trie[26];
            word = "";
        }
    }
    public List<String> findWords(char[][] board, String[] words) {
        // 将words构建成字典树
        Trie root = new Trie();
        for(String word : words) {
            add(root, word);
        }
        boolean[][] signed = new boolean[board.length][board[0].length];
        HashSet<String> ans = new HashSet<>();
        // 在board中选取点在Tire中走看是否存在路径
        for(int i = 0; i < board.length; i++) {
            for(int j = 0; j < board[0].length; j++) {
                signed[i][j] = true;
                dfs(root, board, signed, ans, i, j);
                signed[i][j] = false;
            }
        }
        return new ArrayList<>(ans);
    }
    
    public void add(Trie root, String word) {
        for(int i = 0; i < word.length(); i++) {
            if(root.children[word.charAt(i) - 'a'] == null) {
                root.children[word.charAt(i) - 'a'] = new Trie();
            }
            root = root.children[word.charAt(i) - 'a'];
        }
        root.word = word;
    }

    /**
     * 深度优先搜索
     * @param root 当前节点
     * @param board 单词网格
     * @param signed 标记数组
     * @param ans 答案
     * @param x 当前位置x坐标
     * @param y 当前位置y坐标
     */
    public void dfs(Trie root, char[][] board, boolean[][] signed, HashSet<String> ans, int x, int y) {
        if(root == null) return;
        if(!Objects.equals(root.word, "")) {
            ans.add(root.word);
        }
        root = root.children[board[x][y] - 'a'];
        for(int i = 0; i < 4; i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if(xx >= 0 && xx < board.length && yy >= 0 && yy < board[0].length && !signed[xx][yy]) {
                char ch = board[xx][yy];
                signed[xx][yy] = true;
                dfs(root, board, signed, ans, xx, yy);
                signed[xx][yy] = false;
            }
        }
        if (root != null && root.word != "") ans.add(root.word);
    }

    public boolean search(Trie root, String word) {
        for(int i = 0; i < word.length(); i++) {
            if(root.children[word.charAt(i) - 'a'] == null) {
                return false;
            }
            root = root.children[word.charAt(i) - 'a'];
        }
        return "".equals(root.word);
    }
相关推荐
步步为营DotNet43 分钟前
深度解析CancellationToken:.NET中的优雅取消机制
java·前端·.net
郝学胜-神的一滴8 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
JH30739 小时前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
颜酱10 小时前
图结构完全解析:从基础概念到遍历实现
javascript·后端·算法
m0_7369191010 小时前
C++代码风格检查工具
开发语言·c++·算法
yugi98783810 小时前
基于MATLAB强化学习的单智能体与多智能体路径规划算法
算法·matlab
Coder_Boy_10 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
DuHz10 小时前
超宽带脉冲无线电(Ultra Wideband Impulse Radio, UWB)简介
论文阅读·算法·汽车·信息与通信·信号处理
invicinble10 小时前
对tomcat的提供的功能与底层拓扑结构与实现机制的理解
java·tomcat
Polaris北极星少女11 小时前
TRSV优化2
算法