leetcode@ [79/140] Trie树应用 Word Search / Word Search II
https://leetcode.com/problems/word-search/
class Solution {
public:
struct Trie{
Trie *next[];
bool isWord;
Trie() {
this->isWord = false;
for(auto &c: next) c = NULL;
}
};
void insert(Trie *root, string word) {
Trie *p = root;
for(auto &c: word) {
int idx = c - 'A';
if(!p->next[idx]) p->next[idx] = new Trie();
p = p->next[idx];
}
p->isWord = true;
}
bool isPrefix(Trie *root, string word) {
Trie *p = root;
for(auto &c: word) {
int idx = c - 'A';
if(!p->next[idx]) return false;
p = p->next[idx];
}
return true;
}
bool isWord(Trie *root, string word) {
Trie *p = root;
for(auto &c: word) {
int idx = c - 'A';
if(!p->next[idx]) return false;
p = p->next[idx];
}
return p->isWord;
}
bool check(vector<vector<char>> &board, int x, int y) {
int m = board.size(), n = board[].size();
if(x< || y< || x>=m || y>=n) return false;
return true;
}
bool dfs(vector<vector<char>> &board, vector<vector<bool>> &vis, Trie *root, string s, int x, int y) {
if(isWord(root, s))
return true;
}
int dir[][] = {{,}, {,}, {-,}, {,-}};
for(int i=;i<;++i) {
int nx = x + dir[i][], ny = y + dir[i][];
if(check(board, nx, ny) && !vis[nx][ny]) {
if(isPrefix(root, s + board[nx][ny])) {
vis[nx][ny] = true;
if(dfs(board, vis, root, s + board[nx][ny], nx, ny)) return true;
vis[nx][ny] = false;
}
}
}
return false;
}
bool exist(vector<vector<char>>& board, string word) {
if (board.empty() || board[].empty() || word.empty()) return false;
string s = "";
int m = board.size(), n = board[].size();
vector<vector<bool>> vis(m, vector<bool>(n, false));
Trie *root = new Trie();
insert(root, word);
for(int i=;i<m;++i) {
for(int j=;j<n;++j) {
//if(isWord(root, s + board[i][j])) return true;
if(isPrefix(root, s)) {
vis[i][j] = true;
if(dfs(board, vis, root, s + board[i][j], i, j)) return true;
vis[i][j] = false;
}
}
}
return false;
}
};
https://leetcode.com/problems/word-search-ii/
struct TriNode {
TriNode *ch[];
bool isWord;
TriNode() : isWord(false) {
for (auto &a : ch) a = NULL;
}
};
class Solution {
public:
void insert(TriNode *root, string word) {
TriNode *p = root;
for (auto &a : word) {
int i = a - 'a';
if (p->ch[i] == NULL) p->ch[i] = new TriNode();
p = p->ch[i];
}
p->isWord = true;
}
bool isPrefix(TriNode *root, string word) {
TriNode *p = root;
for (auto &a : word) {
int i = a - 'a';
if (p->ch[i] == NULL) return false;
p = p->ch[i];
}
return true;
}
bool isWord(TriNode *root, string word) {
TriNode *p = root;
for (auto &a : word) {
int i = a - 'a';
if (p->ch[i] == NULL) return false;
p = p->ch[i];
}
return p->isWord;
}
bool isValid(vector<vector<char>> &board, int x, int y) {
int m = board.size(), n = board[].size();
if (x < || x >= m || y < || y >= n) return false;
return true;
}
void dfs(vector<vector<char>> board, vector<vector<int>> visit, set<string> &st, string s, TriNode *root, int x, int y) {
if(!isPrefix(root, s)) return;
if(isWord(root, s)) {
st.insert(s);
}
int dx[] = {, -, , };
int dy[] = {, , , -};
int xx, yy;
for (int i = ; i < ; ++i) {
xx = x + dx[i]; yy = y + dy[i];
if (isValid(board, xx, yy) && !visit[xx][yy]) {
visit[xx][yy] = ;
dfs(board, visit, st, s + board[xx][yy], root, xx, yy);
visit[xx][yy] = ;
}
}
}
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
vector<string> res; res.clear();
if (board.empty() || board[].empty() || words.empty()) return res;
TriNode *root = new TriNode();
for(auto &word : words) insert(root, word);
int m = board.size(), n = board[].size();
vector<vector<int>> visit(m, vector<int>(n, ));
string s="";
set<string> st; st.clear();
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
visit[i][j] = ;
dfs(board, visit, st, s + board[i][j], root, i, j);
visit[i][j] = ;
}
}
for (auto &word : st) res.push_back(word);
return res;
}
};
leetcode@ [79/140] Trie树应用 Word Search / Word Search II的更多相关文章
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- leetcode 211. Add and Search Word - Data structure design Trie树
题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- LeetCode 79 Word Search(单词查找)
题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...
- [leetcode trie]211. Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [LeetCode] 79. 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]79.Search Word 回溯法
/** * Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- leetcode@ [211] Add and Search Word - Data structure design
https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...
随机推荐
- 通过SQL Server 2008 访问MySQL(转)
在公司中经常会遇到部署多种数据库环境的情况,对于开发人员来说经常在不同数据库之间转换确实有些繁琐,本篇将介绍从SQL Server 操作MySQL 数据库的方法. 数据库测试环境 1. SQL Ser ...
- ppshu
全部书籍已经下载完毕! http://3cvpkfx4gdnkcduj.onion/ https://3cvpkfx4gdnkcduj.onion.cab/ https://3cvpkfx4gdnkc ...
- C++转换unicode utf-8 gb2312编码
windows开发环境下用VC++6.0 对unicode .utf-8. gb2312 三种编码格式之间的转换方法: #include <iostream> #include <s ...
- ajax返回正个页面
- POJ 1220 NUMBER BASE CONVERSION(较复杂的进制转换)
题目链接 题意 : 给你一个a进制的数串s,让你转化成b进制的输出. A = 10, B = 11, ..., Z = 35, a = 36, b = 37, ..., z = 61,0到9还是原来的 ...
- Redis hash数据类型操作
Redis hash是一个string类型的field和value的映射表.一个key可对应多个field,一个field对应一个value.将一个对象存储 为hash类型,较于每个字段都存储成str ...
- Adobe Acrobat XI Pro安装破解
注册机使用说明: Install Instructions: (Read carefully!) 安装说明(仔细阅读!) 1. Disable your Network card or pull th ...
- linux 新建用户、用户组 以及为新用户分配权限
Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号 一方面可以帮助系统管理员对使用系统的用户进 ...
- PHP array_chunk() 函数
今天在CSDN上,看到了一个问题 一维数组 PHP code array('0'=>'a',1=>'b',2=>'c',3=>'d',4=>'e',5=>'f' ...
- 高难度(1)常用的AR构架或库
Layar http://www.layar.com/ Layar旨在打造的一个开放的增强现实的平台,任何第三方都可以通过Layar的开发接口来打造基于Layar的自己的增强现实应用. 高通AR开发包 ...