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 ...
随机推荐
- HTTP RFC解析
HTTP协议(HyperText Transfer Protocol,超文本传输协议)HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出, ...
- Spring MVC RESTful
REST: 即 Representational State Transfer,(资源)表现层状态转化是目前最流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便 具体说,就是 HTTP ...
- DevExpress的提示框
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- hive spark版本对应关系
查看hive source下面的pom.xml,可以找到官方默认发布的hive版本对应的spark版本,在实际部署的时候,最好按照这个版本关系来,这样出现兼容问题的概率相对较小. 下面面列出一部分对应 ...
- Python网络爬虫之requests模块
今日内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 知识点回顾 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 引入 ...
- SOA解决方案Dubbo学习入门
1.面向服务架构SOA简介 SOA:Service Oriented Architecture 也即现在常被提及的面向服务编程架构,其相对于传统的垂直架构来说是一种比较新的架构.
- [Unity工具]查找GameObject在场景中所有被引用的地方
参考链接: https://blog.csdn.net/hjzyzr/article/details/53316919?utm_source=blogxgwz4 https://blog.csdn.n ...
- [Unity基础]镜头管理类
一个游戏中可能会有各种类型的镜头,例如有时候是第一人称,有时是第三人称,有时又会给个特写等等,因此可以定义一个镜头类型枚举,在不同的场合进行切换,管理起来很方便. CameraManager.cs u ...
- IdentityServer4 接口说明
在.net core出来以后很多人使用identityServer做身份验证. ids4和ids3的token验证组件都是基于微软的oauth2和bearer验证组件.园子里也很多教程,我们通过教程了 ...
- 56.纯 CSS 描述程序员的生活
原文地址:https://segmentfault.com/a/1190000015316996 感想:动画加延时,white-space: pre; 保留HTML p 中刻意留下的空白. HTML ...