79. 212. Word Search *HARD* -- 字符矩阵中查找单词
79. Word Search
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
word = "ABCCED"
, -> returns true
,
word = "SEE"
, -> returns true
,
word = "ABCB"
, -> returns false
.
class Solution {
public:
int d[][] = {{, }, {-, }, {, }, {, -}}; bool isValid(int m, int n, int x, int y)
{
return x >= && x < m && y >= && y < n;
} bool check(vector<vector<char>>& board, string word, int m, int n, int x, int y, int l, int k)
{
if(k == l)
return true;
if(!isValid(m, n, x, y) || board[x][y] != word[k])
return false;
int tx, ty, i, j;
board[x][y] = '\0';
for(i = ; i < ; i++)
{
tx = x + d[i][];
ty = y + d[i][];
if(check(board, word, m, n, tx, ty, l, k+))
return true;
}
board[x][y] = word[k];
return false;
} bool exist(vector<vector<char>>& board, string word) {
int m = board.size(), l = word.length();
if( == m || == l)
return false;
int n = board[].size(), i, j;
for(i = ; i < m; i++)
{
for(j = ; j < n; j++)
{
if(check(board, word, m, n, i, j, l, ))
return true;
}
}
return false;
}
};
212. Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board.
Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
For example,
Given words = ["oath","pea","eat","rain"]
and board =
[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]
Return ["eat","oath"]
.
Note:
You may assume that all inputs are consist of lowercase letters a-z
.
You would need to optimize your backtracking to pass the larger test. Could you stop backtracking earlier?
If the current candidate does not exist in all words' prefix, you could stop backtracking immediately. What kind of data structure could answer such query efficiently? Does a hash table work? Why or why not? How about a Trie? If you would like to learn how to implement a basic trie, please work on this problem: Implement Trie (Prefix Tree) first.
class Solution {
public:
int dir[][] = { {,},{-,},{,},{,-} }; class Node
{
public:
Node* ch[];
bool isWord;
string word;
Node()
{
memset(ch, , sizeof(ch));
isWord = false;
word = "";
}
}; void createTree(vector<string>& words, Node *root)
{
int n = words.size(), i;
for (i = ; i < n; i++)
{
Node *p = root;
for (int j = ; j < words[i].length(); j++)
{
if (p->ch[words[i][j] - 'a'] == NULL)
{
Node *t = new Node();
p->ch[words[i][j] - 'a'] = t;
}
p = p->ch[words[i][j] - 'a'];
}
p->isWord = true;
p->word = words[i];
}
} void find(vector<vector<char>>& board, Node *node, int x, int y, vector<string>& ans)
{
int n = board.size(), m = board[].size();
if (x < || x >= n || y < || y >= m || board[x][y] == '\0')
return;
if (node->ch[board[x][y] - 'a'])
node = node->ch[board[x][y] - 'a'];
else
return;
if (true == node->isWord)
{
ans.push_back(node->word);
node->isWord = false;
}
char c = board[x][y];
board[x][y] = '\0';
for (int i = ; i < ; i++)
{
int tx = x + dir[i][], ty = y + dir[i][];
find(board, node, tx, ty, ans);
}
board[x][y] = c;
} vector<string> findWords(vector<vector<char>>& board, vector<string>& words)
{
vector<string> ans;
int n = board.size();
if (n <= )
return ans;
Node *root = new Node();
createTree(words, root);
for (int i = ; i < n; i++)
{
for (int j = ; j < board[].size(); j++)
{
find(board, root, i, j, ans);
}
}
return ans;
}
};
79. 212. Word Search *HARD* -- 字符矩阵中查找单词的更多相关文章
- 79. Word Search在字母矩阵中查找单词
[抄题]: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...
- 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. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- [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 ...
- 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 词语搜索 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 ...
- 212. Word Search II
题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
随机推荐
- 终端、shell、bash的区别联系
最佳答案 终端,即所谓的命令行界面,又称命令终端,用户输入shell命令用的窗口,跟Windows里的DOS界面差不多. shell,Shell就是用户和操作系统之间的壳,中介,GUI和CLI都算是S ...
- Keepalive
https://en.wikipedia.org/wiki/Keepalive Description A keepalive signal is often sent at predefined i ...
- [HDOJ2717]Catch That Cow
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Servlet异常及其生命周期
Servlet 异常 在javax.servlet包中定义了两个异常类 ServletException类 ServletException类定义了一个通用的异常,可以被init().service( ...
- Servlet&jsp基础:第五部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 用iconv指令解决utf8和gb18030编码间转换
Linux显示在Windows编辑过的中文就会显示乱码是由于两个操作系统使用的编码不同所致.Linux下使用的编码是utf8,而Windows使用的是gb18030. 解决方案: 在终端中,进入到 ...
- C语言中strdup函数使用方法
头文件:#include <string.h> 定义函数:char * strdup(const char *s); 函数说明:strdup()会先用malloc()配置与参数s 字符串相 ...
- 【BZOJ】4636: 蒟蒻的数列
4636: 蒟蒻的数列 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 145 Solved: 71[Submit][Status][Discuss] ...
- WdatePicker.js 日历点击时,触发自定义方法 ,可以调用自己的函数。
问题: 在选择日期后,没有提交按钮,得到日期后,就可以把日期传到后台,然后就可以得到数据. 方法: 在input 标签中加入onfocus ,就可以了. wdatePicker();可以自定义事件函数 ...
- javascript学习-原生javascript的小特效(原生javascript实现链式运动)
以下代码就不详细解析了,在我之前的多个运动效果中已经解析好多次了,重复的地方这里就不说明了,有兴趣的童鞋可以去看看之前的文章<原生javascript的小特效> <!DOCTYPE ...