美文网首页
leetcode212 单词搜索II

leetcode212 单词搜索II

作者: 奥利奥蘸墨水 | 来源:发表于2020-04-12 22:39 被阅读0次

题目

单词搜索II

暴力解法

暴力解法就是用leetcode79 单词搜索这题的搜索一个单词的dfs方法,挨个搜索每个单词。

字典树

  • 将所有单词构造成一颗字典树,每一个节点加一个是否是叶子的标记位
  • dfs来搜索这个字典树
    注意:搜到一个单词节点的时候,不要返回。因为有可能这种情况:["abc","abcd"]。
字典树代码
struct MyTreeNode{
    int is_leaf;
    MyTreeNode* children[26];

    MyTreeNode() {
        is_leaf = 0;
        for (int i = 0; i < 26; i++) {
            children[i] = nullptr;
        }
    }
};

class Solution {
private:
    MyTreeNode *root[26];
    vector<string> res;
    int row;
    int col;

    void dfs_for_create_tree(int k, string& str, MyTreeNode* &cur_root) {
        if (cur_root == nullptr) {
            cur_root = new MyTreeNode();
        } 
        if (k == str.size() - 1) {
            cur_root->is_leaf++;
        }

        if (k + 1 < str.size()){
            dfs_for_create_tree(k + 1, str, cur_root->children[str[k + 1] - 'a']);
        }
    }

    void dfs_for_searh_tree(int cur_i, int cur_j, string cur_s, MyTreeNode* &cur_root, vector<vector<char>>& board) {
        if (cur_root == nullptr) {
            return;
        }

        if (cur_root->is_leaf) {
            res.push_back(cur_s);
            cur_root->is_leaf--;
        }

        int next[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

        char ch = board[cur_i][cur_j];
        board[cur_i][cur_j] = '1';

        for (int i = 0; i < 4; i++) {
            int next_i = cur_i + next[i][0];
            int next_j = cur_j + next[i][1];

            if (next_i < 0 || next_i >= row || next_j < 0 || next_j >= col) {
                continue;
            }

            char next_ch = board[next_i][next_j];
            if (next_ch == '1') {
                continue;
            }

            dfs_for_searh_tree(next_i, next_j, cur_s + next_ch, cur_root->children[next_ch - 'a'], board);        
        }
        board[cur_i][cur_j] = ch;
    }
public:
    vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
        if (board.empty()) {
            return {};
        }

        for (auto & str : words) {
            dfs_for_create_tree(0, str, root[str[0] - 'a']);
        }
        
        row = board.size();
        col = board[0].size();

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                string s;
                s += board[i][j];
                dfs_for_searh_tree(i, j, s, root[board[i][j] - 'a'], board);
            }
        }

        return res;
    }
};

相关文章

  • [Leetcode212](python):单词搜索II

    1. 题目来源 分类:字典树 Leetcode212:单词搜索II 2. 题目描述 给定一个二维网格 board ...

  • leetcode212 单词搜索II

    题目 单词搜索II 暴力解法 暴力解法就是用leetcode79 单词搜索这题的搜索一个单词的dfs方法,挨个搜索...

  • 算法 leetcode212 单词搜索 II

    给定一个 m x n 二维字符网格 board 和一个单词(字符串)列表 words,找出所有同时在二维网格和字典...

  • 212. 单词搜索 II

    给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词。单词必...

  • leetcode 单词搜索 II python

    第一版先来个爆搜的,对于每一个单词,先找找有没有跟第一个字母相等的,找到后开始向四周找这个单词。后来超时。第二版,...

  • LeetCode-140-单词拆分 II

    LeetCode-140-单词拆分 II 140. 单词拆分 II[https://leetcode-cn.com...

  • LeetCode 212.单词搜索 II(212.Word Se

    212. 单词搜索 II 给定一个二维网格 **board **和一个字典中的单词列表 words,找出所有同时在...

  • 力扣 212 单词搜索 II

    题意:给定一个字典和一个字符矩阵,在矩阵找出所有字典中的字符串 思路: 用trie把字典中的单词加入trie中 遍...

  • LintCode 132. 单词搜索 II

    题目描述 给出一个由小写字母组成的矩阵和一个字典。找出所有同时在字典和矩阵中出现的单词。一个单词可以从矩阵中的任意...

  • 第五课 ПЯТЫЙ УРОК

    单词 Слова: пятый(数)第五 говорить (动、II)说话、说 стоять (动、II、不及)...

网友评论

      本文标题:leetcode212 单词搜索II

      本文链接:https://www.haomeiwen.com/subject/mvpimhtx.html