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 ...
随机推荐
- How To Use RUN_PRODUCT In Oracle Forms
Run_Product is used to run Oracle Reports (RDF/REP files) in Oracle Forms. It invokes one of the sup ...
- HDU 5724 Chess(国际象棋)
HDU 5724 Chess(国际象棋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- windows上配置git
windows上配置git1.下载mysisigit进入http://msysgit.github.io/,下载,安装,下一步下一步即可. 2.下载tortoisegit进入http://downlo ...
- XAF实现运行时填加验证规则并保存到数据库中
有几种方法可以用来声明一个验证规则.最常用的方法是使用对应的Attribute来定义.详见这里.验证模块还允许您通过在业务类实现 IRuleSource 接口定义自定义的验证规则的来源. IRuleS ...
- ASP.NET 2.0 异步页面原理浅析 [1]
与 ASP.NET 1.0 相比,ASP.NET 2.0 的各方面改进可以说是非常巨大的.但就其实现层面来说,最大的增强莫过于提供了对异步页面的支持.通过此机制,编写良好的页面可以将数据库.WebSe ...
- FTP常用故障代码注解
FTP错误列表 出处:http://bbs.enet.com.cn/UserControl?act=13&threadID 作者: |秒杀』| 详细的FTP错误列表 Restart marke ...
- QQServer_update
import java.awt.*; import javax.swing.*; import java.net.*; import java.io.*; import java.awt.event. ...
- nodejs学习笔记<一>安装及环境搭建
零零散散学了几天nodejs,进度一直停滞不前,今天沉下心来好好看了下nodejs的介绍和代码.自己也试着玩了下,算是有点入门了. 这里来做个学习笔记. ——————————————————————— ...
- 9月java货车版速记
运算符的优先级java自带的方法正则表达式数组和二维数组:数组遍历,填充数组,数组排序,复制数组,数组查询数组算法:冒泡,选择,反转,快速类和对象:封装,继承,多态,this关键字,抽象类和接口重写和 ...
- Python学习笔记2—内置函数
函数的使用 官方文档:https://docs.python.org/2/library/functions.html