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 ...
随机推荐
- sqlserver数据库设计完整性与约束
use StudentManageDB go --创建主键约束 if exists(select * from sysobjects where name='pk_StudentId') alter ...
- 您真的会修改Active Directory域控制器计算机名称吗
从我开始做微软这行开始,就经常听说某某公司由于什么原因需要修改Active Directory域控制器计算机名称,但发现好多公司都是直接修改,导致了各种奇葩的问题,今天就给大家推荐一个修改Active ...
- 【 MAKEFILE 编程基础之四】详解MAKEFILE 函数的语法与使用!
本站文章均为 李华明Himi 原创,转载务必在明显处注明: 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/gcc-makefile/771.html ...
- C#存储过程 传入参数 传出参数 结果集
作者:卞功鑫 转载请保留:http://www.cnblogs.com/BinBinGo/p/6400928.html //1 连接字符串 string connectionString = &quo ...
- BCGcontrolBar(二) 改变RIBBON字体
在xp系统下 BCGcontrolBar字体会发虚 这时候重新设置下字体就行了 在单文档的MainFram的onCreate中加入 CFont *font=m_wndRibbonBar.GetFont ...
- 安装配置Glusterfs
软件下载地址:http://bits.gluster.org/pub/gluster/glusterfs/3.4.2/x86_64/ 192.168.1.11 10.1.1.241 glusterfs ...
- Heartbeat+DRBD+MFS高可用
Heartbeat+DRBD+MFS高可用. 前言:MFS系统简介 组件名称及作用 管理服务器(Managing Server) 简称Master Server 这个组件的角色是管理整个mfs文件 ...
- File Input Features
文件输入功能 1.该插件将将一个简单的 HTML 文件输入转换为高级文件选取器控件.将有助于对不支持 JQuery 或 Javascript 的浏览器的正常 HTML 文件输入进行回退. 2.文件输入 ...
- JAVA获取文件夹下所有的文件
package com.test; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; im ...
- 2.App爬取相关库的安装(安装mitmproxy)
mitmproxy 是一个支持HTTP 和HTTPS 的抓包程序,类似fiddler,Charles的功能(它通过控制台的形式操作). mitmproxy 两个关键的组件:mitmdump 和 mit ...