超时,用了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. 学的一点点ps

    从C语言的代码中解脱开始学ps,看到色彩鲜明的东西,心里只有那么爽.哈哈.只学习3天,虽然只是一些皮毛,可还是学到了一些以前不知道的东西.让我对ps多了很多兴趣,决定以后要自学更多的ps技能.要给图片 ...

  2. 内部类 & 泛型

    内部类 主要作用 1. 内部类, 主要用于事件监听的方法实现.2. 用于多继承 注: 主要还是 1 用的比较多. 参考 : head first java (第12章) 泛型 head first j ...

  3. github文件上传及github pages博客搭建教程

    一.与github建立连接 1.安装node.js和git 2.桌面新建文件夹[github],右键“git bash here” 3.注册github账号,新建仓库“new repository”, ...

  4. HTML 文本格式化<b><big><em><i><small><strong><sub><sup><ins><del>

    <b> 标签-粗体 定义和用法: <b>标签规定粗体文本. 提示和注释 注释:根据 HTML5 规范,在没有其他合适标签更合适时,才应该把 <b> 标签作为最后的选 ...

  5. 第一个Android程序

    MainActivity.java package com.example.crystalball; import android.support.v4.app.Fragment; import an ...

  6. CentOS7下安装Tomcat

    1.下载tomcat. 测试tomcat版本为:apache-tomcat-8.5.6.tar.gz.下载地址:http://tomcat.apache.org/download-80.cgi. 2. ...

  7. WCF初探-4:WCF消息交换模式之请求与答复模式

    请求与答复模式( Request/Reply) 这种交换模式是使用最多的一中,它有如下特征: 调用服务方法后需要等待服务的消息返回,即便该方法返回 void 类型 相比Duplex来讲,这种模式强调的 ...

  8. iOS开发Swift篇—(三)字符串和数据类型

    iOS开发Swift篇—(三)字符串和数据类型 一.字符串 字符串是String类型的数据,用双引号""包住文字内容  let website = "http://www ...

  9. ionic 安装本地插件极光推送

    问题:按照官方文档的步骤 假如把插件保存到了D:\push\jpush,当执行到 cordova plugin add D:\push\jpush 的时候,ionic 不是从本地目录安装,而是从reg ...

  10. 配置Entity Framework连接Sql Server出现的一个异常

    异常:The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFrame ...