一、题目说明

题目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的更多相关文章

  1. [刷题] 79 Word Search

    要求 给定一个二维平面的字母和一个单词,从一个字母出发,横向或纵向连接二维平面上的其他字母 同一位置的字母只能使用一次 示例 board = [   ['A','B','C','E'],   ['S' ...

  2. 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 ...

  3. [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 ...

  4. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  5. 【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 ...

  6. [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 ...

  7. 79. Word Search在字母矩阵中查找单词

    [抄题]: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...

  8. Leetcode#79 Word Search

    原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...

  9. 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 ...

随机推荐

  1. Rabbitmq | ConnectionException:Connection refused: connect

    案例 今天完成了Rabbitmq的搭建,调用本地mq服务器是可以的,但是在本地调用远程mq发现出现了connectionException异常,使用的是默认端口5672,具体情况如下图 解决方案 修改 ...

  2. 快速理解DevOps概念和意义-兼谈SRE

    最近几年,由于负责的范围的变化.工作逐渐从某个IT领域或者部门,开始关注到整个IT体系的运转和管理.中间也遇到不少困难,同时也有机会去从更高的层面去学习和实践IT治理.文章主要是总结一下我对DevOp ...

  3. SSH(二)

    SSH框架整合的系统架构,Action.Service.Dao.SessionFactory.DataSource都可以作为Spring的Bean组件管理 使用HibernateDaoSupport基 ...

  4. Django项目在Linux服务器上部署和躺过的坑

    引言 在各方的推荐下,领导让我在测试环境部署之前开发的测试数据预报平台.那么问题来了,既然要在服务器上部署, 就需要准备: 1.linux服务器配置 2.linux安装python环境搭建与配置 3. ...

  5. 不要被C++“自动生成”所蒙骗

    http://www.cnblogs.com/fanzhidongyzby/archive/2013/01/12/2858040.html C++对象可以使用两种方式进行创建:构造函数和复制构造函数. ...

  6. VC简单实现播放音乐

    #define _CRT_SECURE_NO_WARNINGS #include <string.h> #include <stdio.h> #include <wind ...

  7. Codeforces_794

    A.统计两个guard之间的钞票数. #include<bits/stdc++.h> #define MOD 1000000009 using namespace std; int a,b ...

  8. CCF_ 201509-3_模板生成系统

    又是一道考验细心和耐心的题,不知道哪里出问题了,一直只有90分 = =! #include<cstdio> #include<iostream> #include<cst ...

  9. 《Python学习手册 第五版》 -第5章 数值类型

    本章是承接第四章整体说明之后,将对”数值类型“展开详细的说明 数值类型这一章主要通过一下几个内容来讲解: 1.数值类型有哪些? 2.表达式运算符:有哪些?有什么规范? 3.数值的显示格式 接下来,从第 ...

  10. Decorator - 装饰器

    装饰器 Decorator, 先来看看对 decorator 这个名词的解释, 一个可调用的对象 A (decorator), 返回另一个可调用的对象 B, 在可调用的对象 C 的定义体之前通过语法 ...