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 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,太水了以至于一遍就AC了。。。
下面是AC的代码:
class Solution {
public:
int **flag;
bool search(vector<vector<char>>& board, string word, int row, int col){
if(row < 0 || row >= board.size() || col < 0 || col >= board[0].size()){
return false;
}
if(word[0] == board[row][col] && flag[row][col] == 0){
if(word.size() == 1){
return true;
}
else{
int length = word.size();
flag[row][col] = 1;
int ret = search(board, word.substr(1, length - 1), row - 1, col)
|| search(board, word.substr(1, length - 1), row, col + 1)
|| search(board, word.substr(1, length - 1), row + 1, col)
|| search(board, word.substr(1, length - 1), row, col - 1);
if(ret == true){
return true;
}
else{
flag[row][col] = 0;
return false;
}
}
}
else{
return false;
}
}
bool exist(vector<vector<char>>& board, string word) {
int row = board.size();
int col = board[0].size();
flag = new int*[row];
for(int i = 0; i < row; i++){
flag[i] = new int[col];
for(int j = 0; j < col; j++){
flag[i][j] = 0;
}
}
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(search(board, word, i, j) == true){
return true;
}
}
}
return false;
}
};
117
LeetCode OJ 79. Word Search的更多相关文章
- 【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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 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 l ...
- 【LeetCode】79. Word Search 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 刷题79. Word Search
一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...
- [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解题报告—— Word Search & Subsets II & Decode Ways
1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...
随机推荐
- css段落(后盾)
- C#存储过程中传入传出参数
作者:卞功鑫 ,转载请保留http://www.cnblogs.com/BinBinGo/p/6399847.html //1 连接字符串 string connectionString = &qu ...
- 第13课 lambda表达式
1. lambda的语法形式:[capture](params) opt -> ret {body;}; (1)capture为捕获列表 ①[].[&]和[=]分别表示不捕获.按引用捕获 ...
- windows 查询文件被什么程序占用
运行Resmon CPU选项卡全选 在[关联的句柄]里查询: 需要的时间挺多的...
- Cookie快速入门实践
第一个servlet[比如是CookieDemo01]中的代码如下: import javax.servlet.http.Cookie; //--------省略若干代码----------- pro ...
- Linux性能优化 第六章 性能工具:磁盘I/O
6.1 磁盘I/O介绍 一般来说,Linux磁盘的每个分区要么包含一个文件系统,要么包含一个交换分区.这些分区被挂载到Linux根文件系统,该系统由/etc/fstab指定.这些被挂载的文件系统包含了 ...
- Android悬浮框,在Service中打开悬浮窗;在Service中打开Dialog;
文章介绍了如何在Service中显示悬浮框,在Service中弹出Dialog,在Service中做耗时的轮询操作: 背景需求: 公司的项目现在的逻辑是这样的:发送一个指令,然后3秒一次轮询去查询这个 ...
- mysql:视图,触发器,事务,存储过程,函数。
一 视图 1 什么是视图:视图其实就是通过查询得到一张表并且保存下来,就是一张虚拟的表,并非真实存在,比如我们将两个表在终端通过(inner join)内链接起来,那么我们得到的这个表就叫做视图,其 ...
- angularjs路由相关知识
angular.module('app').config(['$stateProvider','$urlRouterProvider',function($stateProvider,$urlRout ...
- linux守护进程与&的区别
1.默认情况下,进程是在前台运行的,这时就把shell占据了(有很多日志打印输出),我们无法进行其他操作.所以对于没有交互的进程,很多时候我们希望将其在后台启动,可以在启动参数的时候加一个&实 ...