一、题目说明

题目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. Shell常用命令之read

    前言 Linux read命令用于从标准输入读取数值.read 内部命令被用来从标准输入读取单行数据.这个命令可以用来读取键盘输入,当使用重定向的时候,可以读取文件中的一行数据. 格式 read [- ...

  2. Percona-XtraDB-Cluster-57 安装操作记录

    一.PXC集群的一些特性 Percona官网服务器位于境外,访问很困难.本次安装使用的是其官网提供的最新版本5.7.23-31.31.1.el7,当前日期为2018.10.10. PXC集群中,存储引 ...

  3. 深入分析Java反射(一)-核心类库和方法

    前提 Java反射的API在JavaSE1.7的时候已经基本完善,但是本文编写的时候使用的是Oracle JDK11,因为JDK11对于sun包下的源码也上传了,可以直接通过IDE查看对应的源码和进行 ...

  4. 深入解读大厂java面试必考基本功-HashMap集合

    课程简介 HashMap集合在企业开发中是必用的集合同时也是面试官面试率很高的集合,因为HashMap里面涉及了很多的知识点,可以比较全面考察面试者的基本功,想要拿到一个好offer,这是一个迈不过的 ...

  5. SpringMVC基础(一)_控制器

    Spring MVC Spring MVC 基于模型-视图-控制器(Model-View-Controller)模式实现,它能够帮你构建灵活和松耦合的应用程序. 1.Spring MVC的请求追踪 每 ...

  6. [python之路]简单介绍

    python介绍 #python是一个什么样的语言?编译型和解释型静态语言和动态语言强类型定义语言和弱类型定义语言python是一门动态解释性的强类型定义语言. #Python的优缺点##优点Pyth ...

  7. LintCode 433. 岛屿的个数(Number of Islands)

    LintCode 433. 岛屿的个数(Number of Islands) 代码: class Solution: """ @param grid: a boolean ...

  8. 使用IDEA详解Spring中依赖注入的类型(上)

    使用IDEA详解Spring中依赖注入的类型(上) 在Spring中实现IoC容器的方法是依赖注入,依赖注入的作用是在使用Spring框架创建对象时动态地将其所依赖的对象(例如属性值)注入Bean组件 ...

  9. 【python爬虫】windoes的爬虫中文乱码现象,通用转码解决

    page = session.get(url="https://www.qidian.com/") page.encoding = page.apparent_encoding p ...

  10. win10电脑搭建网站

    新建网站之后,IIS错误提示是:在计算机“.”上没有找到服务W3SVC,需要在“启动或关闭windows功能”添加.net 3.5下面的两个程序. https://img-blog.csdn.net/ ...