【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 ...
随机推荐
- junit入门
一.简介JUnitJUnit是一个开源的java单元测试框架.在1997年,由 Erich Gamma 和 Kent Beck 开发完成.这两个牛人中 Erich Gamma 是 GOF 之一:Ken ...
- java 运行指定类的main函数
运行jar文件的方法是: java -jar xxx.jar 但是有时,我们希望运行里面的具体某个类,这时可以通过: java -cp xxx.jar xxx.com.xxxx 它会找到这个类的ma ...
- Bitcask 存储模型
Bitcask 存储模型 Bitcask 是一个日志型.基于hash表结构的key-value存储模型,以Bitcask为存储模型的K-V系统有 Riak和 beansdb新版本. 日志型数据存储 何 ...
- js日期格式化函数
/** * @author ocq * * 对Date的扩展,将 Date 转化为指定格式的String * 月(M).日(d).12小时(h).24小时(H).分(m).秒(s).周(E).季度(q ...
- linux下gimp的使用
参考资料: http://wenku.baidu.com/view/345c525f804d2b160b4ec070.html 没有视频, 只靠自己摸索使用... 参考文章: http://www.3 ...
- hdu4952 Number Transformation (找规律)
2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...
- ExtJS -- Grid 文本居中显示
Reference: http://stackoverflow.com/questions/6561120/extjs-4-apply-defaults-to-all-columns-in-a-gri ...
- JCarouselLite--帮助文档
jcarousellite是一款jquery插件,可以控制文档元素滚动,丰富的参数设置可以控制滚动的更多细节,是一款不可多得的滚动插件. ------------------ 官网地址:http:// ...
- SQL 获取查询IO信息
DBCC DROPCLEANBUFFERS --清空缓存 SET STATISTICS IO ON --开启IO统计 SET STATISTICS TIME ON -- 开启耗时统计 <code ...
- iOS之下拉放大,上推缩小,一个方法搞定
先来看看效果吧. 讲讲大概的实现思路:1、创建头部的视图和tableview,需要注意的是tableview要设置contentInset,contentInsent 的顶部要和头部视图的背景图的高度 ...