LeetCode OJ: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.
简单的单词查找,在上下左右四个方向想dfs就可以了,代码如下,注意函数之间传递引用否则会超时的:
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
if(!word.size())
return false;
int szHor = board.size();
if(!szHor) return false;
int szVer = board[].size();
if(!szVer) return false;
vector<vector<bool>> check(szHor, vector<bool>(szVer, false));
for(int i = ; i < szHor; ++i){
for(int j = ; j < szVer; ++j){
if(board[i][j] == word[]){
check[i][j] = true;
if(word.size() == || search(word.substr(), i, j, check, board))
return true;
check[i][j] = false;
}
}
}
return false;
}
bool search(string word, int i, int j, vector<vector<bool>> & check, vector<vector<char>> & board)
{
vector<vector<char>> direction{{-, }, {, }, {, -}, {, }};
for(int k = ; k < ; ++k){
int ii = direction[k][] + i;
int jj = direction[k][] + j;
if(ii >= && ii < board.size() &&
jj >= && jj < board[].size() &&
board[ii][jj] == word[] &&
check[ii][jj] == false){
check[ii][jj] = true;
if(word.size() == || search(word.substr(), ii, jj, check, board))
return true;
check[ii][jj] = false;
}
}
return false;
}
};
LeetCode OJ:Word Search(单词查找)的更多相关文章
- [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 OJ] Word Search 深度优先搜索DFS
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单词搜索 (C++)
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- [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 ...
- 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 词语搜索之二
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 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 词语搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
随机推荐
- CoreThink开发(十三)增加页面加载动画
效果: 加载动画是由jquery和fakeloader这个js库实现的. 其实这个也可以做成一个插件,用数据库记录是否开启,选择动画的样式,那样扩展性会更好. 源码资源已经上传在我的csdn下载中. ...
- 简明python教程四-----模块
模块基本是一个包含了所有你定义的函数和变量的文件.为了在其他程序中重用模块,模块的文件名必须以.py为扩展名. #!/usr/bin/python #Filename:using_sys.py imp ...
- 使用反射实现 webdriver page 类
这个类的目的是为了简化page类的实例化,只需要定义public page成员变量 然后再 启动driver后 通过反射实例化page 后面可以直接点出page实例 package crazy.sel ...
- yum安装mysql5.6
1.检查系统是否安装其他版本的MYSQL数据 yum list installed | grep mysql yum -y remove mysql-libs.x86_64 2.安装及配置 wget ...
- Linux工作管理 jobs、fg、bg、nohup命令
概述 在Linux 中我们登陆了一个终端,已经在执行一个操作,可以通过一定的操作或命令在不关闭当前操作的情况下执行其他操作. 例如,我在当前终端正在 vi 一个文件,在不停止 vi 的情况下,如果我想 ...
- HCNP学习笔记之ICMP协议与ping原理以及用Python实现ping
一.ICMP协议分析 ICMP:Internet控制报文协议.由于IP协议并不是一个可靠的协议,它不保证数据被成功送达,那么,如何才能保证数据的可靠送达呢? 这里就需要使用到一个重要的协议模块ICMP ...
- 《网络攻防》 MSF基础应用
20145224陈颢文 <网络攻防>MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode: exploit:攻击手段,是能使攻击武器(payloa ...
- QString类
1.将QString所有字母大写 QString.toUpper() 2.将QString所有字母小写 QString.toLower() 3.获取字符串的字符数 QString.length() 4 ...
- react-native run-android Starting: Intent Error type 3 Error: Activity class does not exist
使用”react-native run-android”命令运行android应用时,如果常常出现如下错误: Starting the app (/home/xxx/soft/sdk//platfor ...
- bower安装使用、git安装、node安装、weui安装开发
bower安装使用以及git安装 bower需要:node 和 git 1.Git安装:(选择第二项:Use Git from the Windows Command Prompt)2.node安装: ...