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;
}
};
LeetCode() Word Search II的更多相关文章
- [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 ...
- Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)
Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- [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 ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- 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 ...
- [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 ...
- [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 ...
- 【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 ...
随机推荐
- XAF How to: 实现一个WCF Application Server 并配置它的客户端应用
本主题描述了如何实现一个 WCF 中间层应用程序服务器及如何配置 XAF客户端连接到此服务器. 注意 本主题演示可以由解决方案向导自动生成的代码.执行操作时,如果你想要在现有的 XAF 解决方案中实现 ...
- 【数位dp】
hdu5787 问:L ~ R有多少个数是K-wolf Number?其中,K-wolf Number的定义是这个数在十进制下,任意相邻的K个字符没有相同的. dp[i][j][k]表示有i个空位可填 ...
- Shell 语法之函数
函数是被赋予名称的脚本代码块,可以在代码的任意位置重用.每当需要在脚本中使用这样的代码块时,只需引用该代码块被赋予的函数名称. 创建函数 格式 function name { commands } n ...
- 【转载】CSS position属性和实例应用
目前几乎所有主流的浏览器都支持position属性("inherit"除外,"inherit"不支持所有包括IE8和之前版本IE浏览器,IE9.IE10还没测试 ...
- 【转】 HTTP 协议简介
一.TCP/IP 协议介绍 在介绍 HTTP 协议之前,先简单说一下TCP/IP协议的相关内容.TCP/IP协议是分层的,从底层至应用层分别为:物理层.链路层.网络层.传输层和应用层,如下图所示: 从 ...
- DOM遍历方法
为标题行添加样式 $(document).ready(function(){ $('th').parent().addClass('table-heading'); $('tr:not([th]):o ...
- VUE应用的一些感受
方便,数据绑定太方便了. 一个组件一个.vue文件特别清晰. 讲真vue比angular好学多了. webpack打包最近看懂,通过一个主文件把require的文件都打进来.业务代码放build里,引 ...
- subplot demo
Y=[6484.05190614446 3479.60374683749 2326.82521799362 862.207727785871 423.711173743815 299.23540931 ...
- Educational Codeforces Round 14 E.Xor-sequences
题目链接 分析:K很大,以我现有的极弱的知识储备,大概应该是快速幂了...怎么考虑这个快速幂呢,用到了dp的思想.定义表示从到的合法路径数.那么递推式就是.每次进行这样一次计算,那么序列的长度就会增加 ...
- 基于TF-IDF值的汉语语义消歧算法
RT,学校课题需要233,没了 话说,窝直接做个链接的集合好了,方便以后查找 特征值提取之 -- TF-IDF值的简单介绍 汉语语义消歧之 -- 句子相似度 汉语语义消歧之 -- 词义消歧简介 c++ ...