刷题79. Word Search
一、题目说明
题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在。可以连续横、纵找。不能重复使用,难度是Medium。
二、我的解答
惭愧,我写了很久总是有问题,就先看正确的写法,下面是回溯法的代码:
class Solution {
public:
int m,n;
//左 上 右 下
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
bool exist(vector<vector<char>>& board,string word){
if(board.empty()||word.empty()){
return false;
}
m = board.size();
n = board[0].size();
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(dfs(board,i,j,word,0)){
return true;
}
}
}
return false;
}
bool dfs(vector<vector<char>>& board,int x,int y,string&word,int pos){
if(word[pos]!=board[x][y]){
return false;
}
if(pos == word.size()-1){
return true;
}
board[x][y] = '.';
for(int i=0;i<4;i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx<m && nx>=0 && ny<n && ny>=0){
if(dfs(board,nx,ny,word,pos+1)){
return true;
}
}
}
board[x][y] = word[pos];
return false;
}
};
性能:
Runtime: 24 ms, faster than 87.44% of C++ online submissions for Word Search.
Memory Usage: 9.8 MB, less than 100.00% of C++ online submissions for Word Search.
三、优化措施
我的思路是用unordered_map<char,vector<vector<int>>> ump;来存储board中所有字符的出现位置,然后从word的第1个开始起查找,用dfs算法(回溯算法)进行匹配,修改并提交了差不多10次,才成功。
class Solution{
public:
bool exist(vector<vector<char>>& board,string word){
if (board.empty() || word.empty()) {
return false;
}
int row = board.size();
int col = board[0].size();
if (row * col < word.length()) {
return false;
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
char ch = board[i][j];
ump[ch].push_back({i,j});
}
}
if(dfs(board,0,0,0,word)){
return true;
}else{
return false;
}
}
bool dfs(vector<vector<char>>& board,int start,int x,int y,string& word){
char ch = word[start];
bool matched = false;
if(ump.count(ch)){
for(auto current: ump[ch]){
int row1 = current[0];
int col1 = current[1];
//是否相邻
if(start==0 || x==row1 && abs(y-col1)==1 || y==col1 && abs(x-row1)==1){
if(board[row1][col1]!='.'){
board[row1][col1] = '.';
if(start<word.size()-1){
matched = dfs(board,start+1,row1,col1,word);
if(matched) return true;
}else if(start==word.size()-1){
return true;
}
board[row1][col1] = ch;
}
}
};
}else{
return false;
}
return false;
};
private:
unordered_map<char,vector<vector<int>>> ump;
};
惭愧的是,性能还不如普通的回溯法:
Runtime: 764 ms, faster than 5.02% of C++ online submissions for Word Search.
Memory Usage: 169.2 MB, less than 16.18% of C++ online submissions for Word Search.
刷题79. Word Search的更多相关文章
- [刷题] 79 Word Search
要求 给定一个二维平面的字母和一个单词,从一个字母出发,横向或纵向连接二维平面上的其他字母 同一位置的字母只能使用一次 示例 board = [ ['A','B','C','E'], ['S' ...
- 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 ...
- [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 ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- 【LeetCode】79. Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- [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 ...
- 79. Word Search在字母矩阵中查找单词
[抄题]: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...
- Leetcode#79 Word Search
原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...
- LeetCode OJ 79. Word Search
题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...
随机推荐
- Java 设计模式之抽象工厂模式
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂.该超级工厂又称为其他工厂的工厂.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 在抽 ...
- js中 call() 和 apply() 方法的区别和用法详解
1.定义 每个函数都包含俩个非继承而来的方法:call() 和 apply() call 和 apply 可以用来重新定义函数的的执行环境,也就是 this 的指向:call 和 apply 都是 ...
- centos7 配置虚拟交换机(物理交换机truck端口设置)(使用brctl)
转自:http://blog.csdn.net/qq_21398167/article/details/46409503 虚拟交换机配置 inux VLAN配置(vconfig) 安装vlan(vco ...
- ReLU函数
Rectifier(neural networks) 在人工神经网络中,rectfier(整流器,校正器)是一个激活函数,它的定义是:参数中为正的部分. , 其中,x是神经元的输入.这也被称为ramp ...
- [github]添加fork me标识
下午用python在命令行画超载鸡,累死,以后慢慢再改吧. 偶然见看到别人博客园右上角有github的fork me图标,就找找,自己也弄上. 直接给官方博客地址:地址 复制添加到需要的页面源码中,把 ...
- POJ_1050_最大子矩阵
http://poj.org/problem?id=1050 这道题是最大子串的扩展,遍历过每一个子矩阵就好了,期间用了最大子串的方法. #include<iostream> #inclu ...
- 关于JAVA中源码级注解的编写及使用
一.注解简介: 1.1.什么是"注解": 在我们编写代码时,一定看到过这样的代码: class Student { private String name; @Override ...
- postman之设置关联
接口关联(上一个接口的返回参数作为下一个接口的入参使用): 一:在第一个接口的test点击Response body:JSON value check和set an environment varia ...
- 解释为什么不能依赖fail-fast
我的观点fail-fast是什么就不多解释了,应该注意到的是(以ArrayList为例):modCount位于AbstractList中, protected transient int modCou ...
- 【python-leetcode206-翻转链表】反转链表
问题描述: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1->NULL进阶:你可 ...