LeetCode() Word Search II
超时,用了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相关推荐
- 【DFS + Backtracking】LeetCode 212. Word Search II
LeetCode 212. Word Search II Solution1:我的答案 暴力搜索..基于第79题的答案,真的是非常之慢啊!!! 快的方法均是基于字典树的方法,真是复杂.. class ...
- 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连
解析 max ksubarray sum: 最大和 of 连续子序列 => 最大和 of k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff: 无queu ...
- [LeetCode] Word Break II 拆分词句之二
[LeetCode] Word Break II 拆分词句之二 Given a string s and a dictionary of words dict, add spaces in s to ...
- leetcode 212. Word Search II | 212. 单词搜索 II(Trie,回溯,DFS)
题目 https://leetcode.com/problems/word-search-ii/ 题解 基于前缀树实现,如果把 Trie 也看做一个特殊的图的话,就是 将两个图同时进行 dfs,就像判 ...
- [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 ...
- [Swift]LeetCode212. 单词搜索 II | Word Search II
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- LeetCode Word Break II
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- 212. Word Search II:多个单词查找
写在前面:这两周持续看花花酱整理的题目列表和视频讲解,也得益于自己持续多年刷题,今天刷这道题目的想法是:会trie树居然就能攻克hard题目!我也离独立攻破hard题目不远了嘛.前段时间看王争在极客时 ...
- LeetCode: Word Search
对于这道题..我也无话可说了..无奈了... 最开始就是用dfs做,写出来之后怎么测试都是time limit exceeded....上网看大家的解决方法都一样啊,都是上下左右检查一遍....我这个 ...
最新文章
- 【Python】百度翻译的爬虫实现(后篇)
- 开源一个天气APP Build with React Native
- Nature综述——真菌的多样性:真菌的高通量测序及鉴定
- 在Stack Overflow如果语言有问题,请写以下英文
- Openstack数据库管理工具alembic更新Enum类型
- 实验六 html网页设计,网页设计.html · 谢泽华/面向对象与软件工程实验二:网页模仿 - Gitee.com...
- 服务器机房有哪些重要系统
- 总结 @ 在 C# string 中的用法
- poj3254 Corn Fields
- 面向对象之: 类空间问题及类之间的关系
- Linux 运维人最常用 150 个命令汇总
- mybatis generator使用总结
- tensorflow模型部署与python java API线上调用
- DFT泄漏 频谱泄漏 (FFT)
- wekan docker安装部署
- 冰汽朋克侦查机器人_冰汽时代机器人与人力效率对比分析 寒霜朋克机器人与人力哪个效率高...
- Dialog确认按钮不dismiss
- 小功能⭐️Untiy组合键检测
- Bomb(数位DP板题)
- gitlab创建merge requests