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 ...
随机推荐
- 深入浅出设计模式——建造者模式(Builder Pattern)
模式动机无论是在现实世界中还是在软件系统中,都存在一些复杂的对象,它们拥有多个组成部分,如汽车,它包括车轮.方向盘.发送机等各种部件.而对于大多数用户而言,无须知道这些部件的装配细节,也几乎不会使用单 ...
- 《BI项目笔记》创建计算成员
也可以利用脚本来生成计算成员
- WebSocket实战之————Workerman服务器的安装启动
安装php apt-get install php5-cli root@iZ23b64pe35Z:/home/www# php -v PHP 5.5.9-1ubuntu4.20 (cli) (buil ...
- Python之SQLAlchemy学习--外键约束问题
以下问题赞为解决: # class User(Base):# __tablename__ = 'user'# #表的结构# id = Column(String(20), primary_key=Tr ...
- Maven 工程错误Failure to transfer org.codehaus.plexus:plexus-io:pom:1.0,Failure to transfer org.codehaus.plexus:plexus-archiver:jar:2.0.1
原本好好的Maven工程却出现了莫名的错误 Failure to transfer org.codehaus.plexus:plexus-archiver:jar:2.0.1 from http:// ...
- openwrt 的依赖找不到问题
Openwrt报告库找不到Package * is missing dependencies for the following libraries: libc.so.6 或其他先检查系统里面有没有这 ...
- Xcode 8 的 Debug 新特性
Contents OverView Static Analyzer Localizability Instance Cleanup Nullablility Runtime Issue View De ...
- 关于makefile
0 Makefile概述 -------------------------------------------------------------------------------- 什么是mak ...
- F2工作流引擎之 工作流运转模型(三)
1流程单起点单终止模型 单起点:一个流程定义必须有且唯一起点 单结束点:一个流程定义必须有且唯一结束点. 约定:提单与结束是每个流程必须有的活动,且唯一只有一个提单和结束. 2串行模型 描述:串行(S ...
- [转载]javaEE规范和SSH三大框架到底有什么关系
转载自: http://blog.csdn.net/bingjing12345/article/details/20641891 1994-2000 年是互联网的大航海时代. 请注意,下面的时间点及其 ...