【leetcode】Word Search
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 =
[
["ABCE"],
["SFCS"],
["ADEE"]
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.
class Solution {
public:
int n,n1,n2;
bool exist(vector<vector<char> > &board, string word) {
n1=board.size();
n2=board[].size();
n=word.length();
bool flag=false;
//vector<vector<bool> > visited(n1,vector<bool>(n2,false));
bool **visited=new bool*[n1];
for(int i=;i<n1;i++)
{
visited[i]=new bool[n2];
for(int j=;j<n2;j++)
{
visited[i][j]=false;
}
}
for(int i=;i<n1;i++)
{
for(int j=;j<n2;j++)
{
if(board[i][j]==word[])
{
//注意visited采用引用传值
flag=flag||dfs(board,word,i,j,visited);
if(flag) return true;
}
}
}
for(int i=;i<n1;i++) delete[] visited[i];
return false;
}
bool dfs(vector<vector<char> > &board,string &word,int i,int j,bool** &visited,int index=)
{
if(board[i][j]!=word[index]||visited[i][j]) return false;
if(index==n-) return true;
visited[i][j]=true;
bool flag1=i+<n1&&dfs(board,word,i+,j,visited,index+);
bool flag2=j+<n2&&dfs(board,word,i,j+,visited,index+);
bool flag3=j->=&&dfs(board,word,i,j-,visited,index+);
bool flag4=i->=&&dfs(board,word,i-,j,visited,index+);
bool result=flag1||flag2||flag3||flag4;
//由于是引用传值,所以没有找到的目标串时要把visited复原
//if(result==false) visited[i][j]=false;
visited[i][j]=result;
return result;
}
};
【leetcode】Word Search的更多相关文章
- 【leetcode】Word Search (middle)
今天开始,回溯法强化阶段. Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...
- 【leetcode】Word Search II(hard)★
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 【LeetCode】74. Search a 2D Matrix
Difficulty:medium More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...
- 【LeetCode】Word Break 解题报告
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【Leetcode】【Medium】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】Word Ladder
Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】1268. Search Suggestions System
题目如下: Given an array of strings products and a string searchWord. We want to design a system that su ...
- 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...
随机推荐
- wampserver 2.5安装pear win8.1
集成环境的悲伤啊~ 本来看到pear想试试 结果发现根本没有go-pear.bat 自己的环境 都是 系统win 8.1 php 5.5.12 mysql 5.6.17 apache 2.4.9 ...
- python递归理解图
递归:下一级只能return给自己的上一级. import re val="9-2*5/3+7/3*99/4*2998+10*568/14" val="9-2*5/3+7 ...
- java 获取文件的最后编辑时间
还是日志的问题,需要把日志文件的一些信息给显示出来,其中就需要显示最后的编辑时间,在网上找的答案... File f = new File(path); SimpleDateFormat sdf = ...
- C# 操作mongodb子文档
var mongoString = "mongodb://jamesbing:123456@localhost:27017"; var host = new TMongodbHos ...
- SQL having 子句
1.为什么存在? 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用. 2.举例子: SELECT Customer,SUM(OrderPrice) FROM Or ...
- Jetty与tomcat的比较
Google 应用系统引擎最初是以 Apache Tomcat 作为其 webserver/servlet 容器的,但最终将切换到 Jetty 上. 这个决定让许多开发人员都诧异的想问:为什么要做这样 ...
- 【转】随机函数 rand() srand() 以及seed的原理
from:http://blog.csdn.net/feige2008/article/details/6943885 标准库<cstdlib>(被包含于<iostream> ...
- 统计学 nested_design 嵌套设计
nested_design 嵌套设计 li_volleyball ,邓邦良 2016年3月6日 嵌套设计 一.基本概念 嵌套设计(nested design)又称为窝设计和套设计,与析因设计的处理不同 ...
- redhat 下 rpm 指令
1.如何安装rpm软件包rmp软件包的安装可以使用程序rpm来完成.执行下面的命令 rpm -i your-package.rpm 其中your-package.rpm是你要安装的rpm包的文件名,一 ...
- CentOS 6.5 zabbix 3.0.4 乱码问题
中文支持 修改web端源文件来开启语言 [root@localhost /]# vim /var/www/html/zabbix/include/locales.inc.php 'zh_CN' =&g ...