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 ...
随机推荐
- deep-learning-frameworks
From: http://venturebeat.com/2015/11/14/deep-learning-frameworks/ Here’s a rundown of some other not ...
- 通过EasyUI Tree说明SQL GUID和自增列ID的使用场景
最新在开发中用到了EasyUI里面的Tree,通过API可以看到这个Tree的数据格式如下: 其中ID比较重要,API也说了,最开始我考虑到GUID比自增ID多占用了一些空间,所以采用的自增ID,测试 ...
- HDU 2222 Keywords Search(查询关键字)
HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- \n ^ \t的使用
\n是换行符: 使用时需要放在字符串里面,成为字符串的一部分(不要嫌乱,这是我之前犯的错误). 示例: >>>print("I am Guido \n what's yon ...
- apt-get下载的文件
1. http://kurenai.elastos.org/2013/05/02/ubuntu-apt-get%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86/ http:// ...
- jquery mobile 学习总结
<!doctype html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...
- 每一个可以移动的棋子都要移动——Every-SG 游戏
先看一个问题 HDU 3595 GG and MM (Every_SG博弈) 题目有N个游戏同时进行,每个游戏有两堆石子,每次从个数多的堆中取走数量小的数量的整数倍的石子.取最后一次的获胜.并且N个游 ...
- spring的初始化bean,销毁bean之前的操作详解
我所知道的在spring初始化bean,销毁bean之前的操作有三种方式: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二种是 ...
- 项目中的五级地址联动效果(js)
我刚开始是的时候是是写了一个sql语句,但是写了5个函数,来联动地址的.后来请教了前段的师傅,用js来写了一个地址联动的. 我使用的是easyui的框架! 地址联动部分html代码! <tr&g ...
- c++11 其他特性(一)
c++11还增加了许多有用的特性,比如: 1. 委托构造函数 如果一个类含有很多构造函数,这些构造函数有一些重复的地方,比如: class A{ public: A(){}; A(int a){ a_ ...