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的更多相关文章

  1. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  2. leetcode 211. Add and Search Word - Data structure design Trie树

    题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...

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

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

  4. LeetCode 79 Word Search(单词查找)

    题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...

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

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

  7. [leetcode]79.Search Word 回溯法

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

  8. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  9. leetcode@ [211] Add and Search Word - Data structure design

    https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...

随机推荐

  1. uva 10534

    一开始WA了  参考了一下   求正反两个方向的最长上升子序列  并分别记录在两个数组中  最后求最大值 #include <iostream> #include <cstdio&g ...

  2. hdu 4557 非诚勿扰

    水题…… 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include<io ...

  3. Weblogic下部署的应用,当更新文件时需要重新安装部署

    JSP页面检查(秒):-1 Servlet重新加载检查(秒):-1 -1说明从不检查,故当更新文件时,需要重新部署,或重新安装部署.

  4. 李洪强iOS开发本人集成环信的经验总结_08_自动登录补充

    李洪强iOS开发本人集成环信的经验总结_08_自动登录补充 来到Appdelegate里面 01 - 遵守自动登录的代理协议 02 - 设置自动登录的代理 03 - 判断与实现  04 - 代理方法的 ...

  5. 双机高可用、负载均衡、MySQL(读写分离、主从自动切换)架构设计

    前几天网友来信说帮忙实现这样一个架构:只有两台机器,需要实现其中一台死机之后另一台能接管这台机器的服务,并且在两台机器正常服务时,两台机器都能用上.于是设计了如下的架构. 架构简介 此架构主要是由ke ...

  6. linux配置防火墙详细步骤(iptables命令使用方法)

    通过本教程操作,请确认您能使用linux本机.如果您使用的是ssh远程,而又不能直接操作本机,那么建议您慎重,慎重,再慎重! 通过iptables我们可以为我们的Linux服务器配置有动态的防火墙,能 ...

  7. inflate方法与findViewById的区别

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来找 re ...

  8. CVE漏洞爬虫java代码依赖-TestNG

    TestNG是Java中的一个测试框架,而该CVE漏洞爬虫示例中所涉及到的java代码中, \Crawler\src\com\***\ThreaderRun.java文件在导入import org.t ...

  9. How to remove spaces of a string with regular expression

    left 's/^\s*//g' right 's/\s*$//g' all 's/\s+//g'

  10. Oracle安装时先决条件检查失败的解决方案

      Oracle安装时先决条件检查失败的解决方案 [java] 安装环境:Win7-64bit专业版,内存6G,硬盘空间足够 安装版本:Oracle Database 11g Release 2 (1 ...