刷题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 ...
随机推荐
- Windows系统以及谷歌浏览器快捷键,控制台常用命令
win10系统 快捷键 win+D 回到桌面 控制台代码(win+R打开控制台) calc 系统计算器 谷歌浏览器快捷键 ctrl+tab 切换标签页 ctrl+ 1/2...9 数字 切换到第几个标 ...
- Redis搭建哨兵模式
一 安装Redis 1. 从https://redis.io/download redis官网下载二进制包安装 例如:wget http://download.redis.io/releases/re ...
- 云原生 - Istio可观察性之监控(四)
作者:justmine 头条号:大数据与云原生 微信公众号:大数据与云原生 创作不易,在满足创作共用版权协议的基础上可以转载,但请以超链接形式注明出处. 为了方便阅读,微信公众号已按分类排版,后续的文 ...
- SpringBoot性能优化之HikariCP连接池
以前一直使用阿里Druid数据库连接池,这段时间听说有个号称速度最快.代码最简的后起之秀——HikariCP,于是动手实践一下 1.依赖如下: <?xml version="1.0&q ...
- num05---装饰模式
当系统需要新的功能的时候,一般都是向旧的类中添加新的代码.比如一个人这个类,需要新增穿衣的功能,那么就会在人这个类中去添加对应的穿衣方法代码,用来增强人这个类的行为功能.但是这样做,会因为新加入的字段 ...
- Go语言实现:【剑指offer】孩子们的游戏
该题目来源于牛客网<剑指offer>专题. 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的 ...
- 1.3创建你的第一个Android项目——Android第一行代码(第二版)笔记
创建HelloWorld项目 如果是第一次,会经过漫长的等待. 启动模拟器 可以用第三方模拟器,也可以用官方集成的 点击后出现如下界面,可创建多个模拟器,如图,我已经创建好了一个,如果你没有,就点击下 ...
- ElasticSearch基础入门学习笔记
前言 本笔记的内容主要是在从0开始学习ElasticSearch中,按照官方文档以及自己的一些测试的过程. 安装 由于是初学者,按照官方文档安装即可.前面ELK入门使用主要就是讲述了安装过程,这里不再 ...
- 最近很火的namebase羊毛, 手把手教你怎么薅
闲话少说直接说步骤: 1. 羊毛 https://www.namebase.io/airdrop 要求条件: 1) 要有github账号 2) 2019年2月之前有16+个follower 3) 要有 ...
- 线段树学习----C语言
/* 线段树学习:如果一个节点为i,那么他的左孩子为2I+1,右孩子为2i+2: */ #include<stdio.h> #define min(a,b) a<b?a:b; ]; ...