超时,用了tire也不行,需要再改。

class Solution {class TrieNode {
public:// Initialize your data structure here.TrieNode() {for(int i=0;i<26;i++)next[i]=NULL;isString = false;}TrieNode *next[26];bool isString;
};class Trie {
public:Trie() {root = new TrieNode();}// Inserts a word into the trie.void insert(string word) {TrieNode* p=root;for(int i=0;i<word.size();i++){if(p->next[word[i]-'a'] == NULL)p->next[word[i]-'a']=new TrieNode();p=p->next[word[i]-'a'];}p->isString=true;}// Returns if the word is in the trie.bool search(string word) {TrieNode* p=root;for(int i=0;i<word.size();i++){if(p->next[word[i]-'a'] == NULL)    return false;p=p->next[word[i]-'a'];}if(p->isString == true)return true;return false;//  return p->isString;}// Returns if there is any word in the trie// that starts with the given prefix.bool startsWith(string prefix) {TrieNode* p=root;for(int i=0;i<prefix.size();i++){if(p->next[prefix[i]-'a'] == NULL)    return false;p=p->next[prefix[i]-'a'];}return true;}private:TrieNode* root;
};// Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");
public:vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {vector<string> res;Trie s;for(auto i:words){if(s.search(i)){s.insert(i);res.push_back(i);}else if(exist(board,i)){res.push_back(i);s.insert(i);}}sort(res.begin(),res.end());return res;}bool exist(vector<vector<char>>& board,string word){for(int i=0;i<board.size();i++)for(int j=0;j<board[0].size();j++)if(exist(board,word,i,j,0))     return true;return false;}bool exist(vector<vector<char>>& board,string word,int x,int y,int pos){if(pos == word.size())  return true;if(x<0 || x>=board.size() || y<0 || y>=board[0].size()) return false;if(word[pos] == board[x][y]){char c=board[x][y];board[x][y]='#';bool res=exist(board,word,x+1,y,pos+1)||exist(board,word,x-1,y,pos+1)||exist(board,word,x,y+1,pos+1)||exist(board,word,x,y-1,pos+1);board[x][y]=c;return res;}return false;}
};

  

转载于:https://www.cnblogs.com/yanqi110/p/5011644.html

LeetCode() Word Search II相关推荐

  1. 【DFS + Backtracking】LeetCode 212. Word Search II

    LeetCode 212. Word Search II Solution1:我的答案 暴力搜索..基于第79题的答案,真的是非常之慢啊!!! 快的方法均是基于字典树的方法,真是复杂.. class ...

  2. 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连

    解析 max ksubarray sum:  最大和 of 连续子序列 =>   最大和 of  k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff:  无queu ...

  3. [LeetCode] Word Break II 拆分词句之二

    [LeetCode] Word Break II 拆分词句之二 Given a string s and a dictionary of words dict, add spaces in s to ...

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

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

  5. [Leetcode] 212. Word Search II 解题报告

    题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...

  6. [Swift]LeetCode212. 单词搜索 II | Word Search II

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  7. LeetCode Word Break II

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  8. 212. Word Search II:多个单词查找

    写在前面:这两周持续看花花酱整理的题目列表和视频讲解,也得益于自己持续多年刷题,今天刷这道题目的想法是:会trie树居然就能攻克hard题目!我也离独立攻破hard题目不远了嘛.前段时间看王争在极客时 ...

  9. LeetCode: Word Search

    对于这道题..我也无话可说了..无奈了... 最开始就是用dfs做,写出来之后怎么测试都是time limit exceeded....上网看大家的解决方法都一样啊,都是上下左右检查一遍....我这个 ...

最新文章

  1. 【Python】百度翻译的爬虫实现(后篇)
  2. 开源一个天气APP Build with React Native
  3. Nature综述——真菌的多样性:真菌的高通量测序及鉴定
  4. 在Stack Overflow如果语言有问题,请写以下英文
  5. Openstack数据库管理工具alembic更新Enum类型
  6. 实验六 html网页设计,网页设计.html · 谢泽华/面向对象与软件工程实验二:网页模仿 - Gitee.com...
  7. 服务器机房有哪些重要系统
  8. 总结 @ 在 C# string 中的用法
  9. poj3254 Corn Fields
  10. 面向对象之: 类空间问题及类之间的关系
  11. Linux 运维人最常用 150 个命令汇总
  12. mybatis generator使用总结
  13. tensorflow模型部署与python java API线上调用
  14. DFT泄漏 频谱泄漏 (FFT)
  15. wekan docker安装部署
  16. 冰汽朋克侦查机器人_冰汽时代机器人与人力效率对比分析 寒霜朋克机器人与人力哪个效率高...
  17. Dialog确认按钮不dismiss
  18. 小功能⭐️Untiy组合键检测
  19. Bomb(数位DP板题)
  20. gitlab创建merge requests

热门文章

  1. BD和DBMS和SQL概念
  2. docker删除本地已下载的镜像
  3. 福禄克网络TIA识别MPTL连接的操作方法
  4. 手机技巧:是否应该一次性给手机充电到100%?看完你就明白了!
  5. 接软件开发项目,你需要知道这些!
  6. mysql009模糊查询like.是否为null
  7. 关于range方法,如果你觉得python很简单就错了
  8. 模式匹配 怎么匹配减号_如何使您的应用导航与用户的思维模式匹配
  9. 如何高效学习前端新知识,我推荐这些~
  10. 回答知乎问题:你写过什么自认为惊艳的诗?