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 ...
随机推荐
- Java I/O 文件加锁,压缩
文件加锁: 文件加锁机制允许我们同步访问某个作为共享资源的文件. public class Test { public static void main(String[] args) throws I ...
- Redis实践操作之—— keyspace notification(键空间通知)
一.需求分析: 设置了生存时间的Key,在过期时能不能有所提示? 如果能对过期Key有个监听,如何对过期Key进行一个回调处理? 如何使用 Redis 来实现定时任务? 二.序言: 本文所说的定时任务 ...
- Python学习(12)日期和时间
目录 Python 日期和时间 时间元组 获取当前时间 获取格式化时间 格式化日历 获取某月日历 Time模块 日历模块 其他相关模块和函数 Python 日期和时间 Python 程序能用很多方式处 ...
- Java编程思想学习笔记_1(Java内存和垃圾回收)
1.Java中对象的存储数据的地方: 共有五个不同的地方可以存储数据. 1)寄存器.最快,因为位于处理器的内部,寄存器按需求分配,不能直接控制. 2)堆栈.位于通用RAM,通过堆栈指针可以从处理器那里 ...
- C++ 多线程中的一个抛出异常
试了一下,和Java完全不同. 注意Java和C++对于多线程里面的一个线程抛出异常的影响,完全不同. Java里面,对于主线程和其他线程完全不受影响: C++里面,整个程序会退出,所有线程都会受影响 ...
- 2 CSS
2 CSS CSS基础 html 在一个网页中负责的事情是一个页面的结构css(层叠样式表) 在一个网页中主要负责了页面的数据样式. 编写css代码的方式: 第一种: 在style标签中编写c ...
- 关于图片加载非常爽的一个三方控件 fresco,一个三fresco
Hi EveryBody 今天来玩一个非常爽的控件 fresco 到底有多爽呢 接着看就知道了 首先 来看看fresco 是个神马东西 https://github.com/facebook/fre ...
- svn设置提交忽略某些文件或文件夹
在svn客户端,想设置忽略提交.class文件,通过 properties > New > Other 添加一个忽略的属性,,还是不行:部分屏蔽了,部分class还是在列表中 再次参考了一 ...
- 网络性能测试工具iperf详细使用图文教程【转载】
原文:http://blog.163.com/hlz_2599/blog/static/142378474201341341339314/ 参考:http://man.linuxde.net/iper ...
- hdu5823(反演dp)
听说3^n也能水过去.. 其实应该是个经典题,求图染色这个np问题. 把问题拆成独立集来进行dp可以在3^n之内水过去. 拆成独立集的时候就发现,等价与一个经典的反演dp问题 然后复杂度就变成了 n* ...