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 ...
随机推荐
- c#day05
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ccc ...
- 第5章 IP地址和子网划分(3)_子网划分
6.子网划分 6.1 地址浪费 (1)IPv4公网地址资源日益紧张,为减少浪费,使IP地址能够充分利用,就要用到子网划分技术. (2)传统上一个C类地址,如212.2.3.0/24,其可用的地址范围为 ...
- 在django中,执行原始sql语句
extra()方法 结果集修改器,一种提供额外查询参数的机制 使用extra: 1:Book.objects.filter(publisher__name='广东人员出版社').extra(where ...
- Java 4- Java 变量类型
Java 变量类型 在Java语言中,所有的变量在使用前必须声明.声明变量的基本格式如下: type identifier [ = value][, identifier [= value] ...] ...
- VMware网络桥接模式与NAT模式共存
对虚拟机有时我们会有一种需求,即需要虚拟机可以作为单独的主机拥有自己的独立IP,也希望宿主机可以通过NAT模式映射端口到虚拟机. 那么此时怎么办那,其实很简单,虚拟机是支持多网卡设置的,只要在虚拟 ...
- day3(第一周)周末作业
1.创建字符串变量的三种写法及其区别# 代码:单引号 ''# 双引号 ""# 多引号 ''' '''# 区别:单引号和双引号没有任何区别,一般用于单行字符:多行字符用多引号.## ...
- python-开放类优化内存性能
开放类:在运行期间,可动态向实例或类添加新成员,方法 1.实例不能添加方法到类,反之可以 class A: pass a = A() a.func = lambda x: x+1 a.func # & ...
- Ruby学习笔记5: 动态web app的建立 (2)
上一节里,我们搭建了一个数据库的结构,并用index验证了request-response cycle,如下图: 1. Add show method into Controller 这一节,我们要继 ...
- 《算法》第五章部分程序 part 1
▶ 书中第五章部分程序,包括在加上自己补充的代码,字母表类,字符串低位优先排序(桶排) ● 字母表类 package package01; import edu.princeton.cs.algs4. ...
- ThinkPHP同时操作多个数据库
除了在预先定义数据库连接和实例化的时候指定数据库连接外,我们还可以在模型操作过程中动态的切换数据库,支持切换到相同和不同的数据库类型.用法很简单, 只需要调用Model类的db方法,用法: $this ...