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

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。

示例:

输入:
words = ["oath","pea","eat","rain"]and board =
[['o','a','a','n'],['e','t','a','e'],['i','h','k','r'],['i','f','l','v']
]输出: ["eat","oath"]

说明:
你可以假设所有输入都由小写字母 a-z 组成。

提示:

  • 你需要优化回溯算法以通过更大数据量的测试。你能否早点停止回溯?
  • 如果当前单词不存在于所有单词的前缀中,则可以立即停止回溯。什么样的数据结构可以有效地执行这样的操作?散列表是否可行?为什么? 前缀树如何?如果你想学习如何实现一个基本的前缀树,请先查看这个问题: 实现Trie(前缀树)。

class Solution {
    class Trie{
    public:
        Trie *children[26]; //指向其子序列 从'a'到'z'
        bool leaf;  //该结点是否是叶子结点
        int idx;  //如果该节点是叶子结点, idx是该单词在vector中的序号
        Trie()
        {
            this->leaf = false;
            this->idx = 0;
            fill_n(this->children, 26, nullptr);
        }
    };
 public:
        void insertWords(Trie *root, vector<string>& words, int idx)
        {
            int pos = 0, len = words[idx].size();
            while(pos < len)
            {
                if(NULL == root->children[words[idx][pos] - 'a'])
                    root->children[words[idx][pos] - 'a'] = new Trie();
                root = root->children[words[idx][pos++] - 'a'];
            }
            root->leaf = true;
            root->idx = idx;
        }

Trie * buildTrie(vector<string>& words)
        {
            Trie * root = new Trie();
            for(int i = 0; i < words.size(); ++i)
                insertWords(root, words, i);
            return root;
        }

void checkWords(vector<vector<char>>& board, int i, int j, int row, int col, Trie *root, vector<string> &res, vector<string>& words)
        {
            if(i <0 || j < 0 || i >= row || j >= col) 
                return;
            if(board[i][j] == 'X') 
                return; 
            if(NULL == root->children[board[i][j] - 'a']) 
                return;
            char temp = board[i][j];
            if(root->children[temp - 'a']->leaf) 
            {
                res.push_back(words[root->children [temp - 'a']->idx]);
                root->children[temp - 'a']->leaf = false; 
            }
            board[i][j] = 'X'; 
            checkWords(board, i-1, j, row, col, root->children[temp-'a'], res, words);
            checkWords(board, i+1, j, row, col, root->children[temp-'a'], res, words);
            checkWords(board, i, j-1, row, col, root->children[temp-'a'], res, words);
            checkWords(board, i, j+1, row, col, root->children[temp-'a'], res, words);
            board[i][j] = temp;
        }
        vector<string> findWords(vector<vector<char>>& board, vector<string>& words) 
        {
            vector<string> res;
           int row = board.size();
           if(0==row) 
               return res;
           int col = board[0].size();
           if(0==col) 
               return res;
           int wordCount = words.size();
           if(0==wordCount) 
               return res;
           Trie *root = buildTrie(words);
           int i,j;
           for(i =0 ; i<row; i++)
           {
               for(j=0; j<col; j++)
               {
                   checkWords(board, i, j, row, col, root, res, words);
               }
           }
           return res;
        }
};

212.单词搜索II相关推荐

  1. 【每日一题】212. 单词搜索 II

    212. 单词搜索 II 题目描述: 给定一个 m x n 二维字符网格 board 和一个单词(字符串)列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过 相邻 ...

  2. leetcode: 212. 单词搜索II

    212. 单词搜索II 来源:力扣(LeetCode) 链接: https://leetcode.cn/problems/word-search-ii 给定一个 m x n 二维字符网格 board和 ...

  3. Java实现 LeetCode 212 单词搜索 II(二)

    212. 单词搜索 II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中&quo ...

  4. LeetCode 212. 单词搜索 II(Trie树+DFS)

    1. 题目 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻&qu ...

  5. 【亡羊补牢】挑战数据结构与算法 第19期 LeetCode 212. 单词搜索 II(字典树,附上JS模板)

    仰望星空的人,不应该被嘲笑 题目描述 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成, ...

  6. leetcode 212:单词搜索II

    题目描述: 给定一个 m x n 二维字符网格 board 和一个单词(字符串)列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过 相邻的单元格 内的字母构成,其 ...

  7. leetcode 212. 单词搜索 II 字典树+深度优先搜索 java代码 详细解释

    给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格 ...

  8. Java实现 LeetCode 212 单词搜索 II

    public class Find2 {public int[] dx={1,-1,0,0};public int[] dy={0,0,1,-1};class Trie{Trie[] tries;St ...

  9. leetcode 212. Word Search II | 212. 单词搜索 II(Trie,回溯,DFS)

    题目 https://leetcode.com/problems/word-search-ii/ 题解 基于前缀树实现,如果把 Trie 也看做一个特殊的图的话,就是 将两个图同时进行 dfs,就像判 ...

  10. 力扣—— 79/212. 单词搜索

    目录 79 单词搜索 212 单词搜索II 79 单词搜索 class Solution(object):#深度搜索def exist(self, board, word):self.flag=0se ...

最新文章

  1. 用linux写python_linux用什么写python
  2. linux clock命令,Linux中clock命令起什么作用呢?
  3. tcp client.cs
  4. 携程SQL面试题忘大牛解答解决思路
  5. java 迭代器的原理_Java集合框架迭代器Iterator实现原理解析
  6. ubuntu18.04如何安装mysql
  7. 用Java搭建一套访问redis的API
  8. linux之登录式shell和非登录式shell
  9. LINQ to Entities 比较日期
  10. SpringMVC 访问html页面乱码
  11. 荣耀V8鸿蒙系统刷机包,华为荣耀V8原版rom系统刷机包_荣耀V8最新版升级包更新下载...
  12. K1-K4分组控制LED
  13. 微信硬件开发系列教程04-新浪云服务器搭建(airkiss/airsync)
  14. Unity-Live2d(表情系统,姿势动作与口型功能的实现)
  15. 进店率、提袋率、客单价
  16. 【漫步计算机系统】:发展概览Ⅲ
  17. ebtables规则arpreply
  18. C# CAD二次开发之基本图形
  19. Windows下 wget 的安装与使用
  20. 从 SAP 帮助文档的页面,谈谈 SAP Content Management 的实现

热门文章

  1. LeetCode 6罗马数字转整数
  2. Java Main 如何是如何被执行的?
  3. ajax ----进度条的原理
  4. Collection与Map
  5. 键盘视频鼠标(KVM)切换器基础知识
  6. Nagios 监控温度感应器
  7. android 组件内部实现触摸事件,更改背景
  8. radiobutton在listview中处理
  9. 报错Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.liuyanzhao.b
  10. python3调用js_关于python3运行JS文件的问题