超时,用了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;
}
};

  

LeetCode() Word Search II的更多相关文章

  1. [LeetCode] Word Search II 词语搜索之二

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

  2. Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)

    Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...

  3. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  4. [LeetCode] Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  5. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  6. Java for 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 mus ...

  7. [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 mus ...

  8. [LeetCode] 212. Word Search II 词语搜索 II

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

  9. 【leetcode】212. Word Search II

    Given an m x n board of characters and a list of strings words, return all words on the board. Each ...

随机推荐

  1. postMessage

    postMessage 父页面 e.target.contentWindow.postMessage(messageData, '*'); /*******onMessage处理******/ fun ...

  2. Nginx模块之————RTMP模块在Ubuntu上以串流直播HLS视频

    Nginx的安装在Ubuntu上以串流直播HLS视频 https://www.vultr.com/docs/setup-nginx-on-ubuntu-to-stream-live-hls-video

  3. 【CSU1808】地铁

    ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci 号线,位于站 ai,bi 之间,往返均需要花费 ti 分钟(即从 ...

  4. javacsript Numnber 对象

    引子: initUppercent = (uploadedSize / file.size * 100).toFixed(2) + '%'; 一.javaScript Number对象 ------- ...

  5. c#中对rgb的使用

    今天发现c#中没有和vb类似的函数RGB(), 后来发现可以通过Color类来实现 从R,G,B的值可以得到一个Color,如: Color c = Color.FromArgb(255,0,0); ...

  6. jquery中通过全局变量来禁止多次ajax请求

    var ajaxstate=false;//定义全局变量,通过这个变量来获得当前的ajax状态 $(function(){ $(document).ajaxStart(function(){ $(&q ...

  7. SCCM 2007日志

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://ycrsjxy.blog.51cto.com/618627/203174      ...

  8. 樱花漫地集于我心,蝶舞纷飞祈愿相随---总结 顕出:void-sampling 显示:void-sampling

    知识点: 分支语句,四则运算,( ̄y▽ ̄)~* 实验过程中遇到的问题及解决方法: 忘记换行,忘记代码,输入法切换(´∀`*) 暂时还得照书写,*★,°*:.☆\( ̄▽ ̄)/$:*.°★* 实验心得体会 ...

  9. Octopus系列之UploadValues异步上载

    不多说了直接上代码 public void ProcessRequest(HttpContext context) { context.Response.ContentType = "tex ...

  10. Windows 下java环境变量的配置(Windows7 ,8,8.1,10)

    Windows 下java环境变量的配置 在“系统”面板的左上角选择“高级系统设置”,在弹出的系统属性中选择”高级“项,然后点击右下角的“环境变量(N)...”,就此进入JAVA环境变量的配置. 如果 ...