【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 ...
随机推荐
- django,python,svn_web
- Java并发编程核心方法与框架-TheadPoolExecutor的使用
类ThreadPoolExecutor最常使用的构造方法是 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAli ...
- android自定义控件(4)-自定义水波纹效果
一.实现单击出现水波纹单圈效果: 照例来说,还是一个自定义控件,观察这个效果,发现应该需要重写onTouchEvent和onDraw方法,通过在onTouchEvent中获取触摸的坐标,然后以这个坐标 ...
- 软件安装失败,导致ubuntu软件中心软件消失
感谢百度上各位IT界朋友的帮助,由于某个软件安装失败,导致ubuntu软件中心软件消失的解决办法: 找百度,有人说, 使用命令:sudo apt-get install software-center ...
- Linux服务器管理: 系统的定时任务crond
cornd 是定时任务的守护进程 这个服务系统是默认启动的 [root@localhost/]#/etc/init.d/crond strat|restart|stop [root@localhos ...
- Linux服务器管理: 系统的进程管理pstree命令
pstree命令是查看进程树或者结构的命令 [root@localhost~]#pstree [选项] 需要注意的是不能将 -p和-u同时使用 如果同时使用前者生效后者无效但并不报错 选项: -p: ...
- 使用jQuery的Scrollify插件实现鼠标滚轮或者手势滑动到页面下一节点部分
有时我们需要做一个单页面介绍产品特性,而单页面内容非常多且页面非常长,为了快速定位到产品特性节点,我们使用js侦听用户滚轮事件,当用户触发滚轮滑动或者使用手势触屏滑动时,即可定位到相应的节点.一款jQ ...
- 处理dataTable的行和列数据
DataTable dt = null; foreach (DataRow dr in dt.Rows) { ; j < dr.ItemArray.Length; j++) { tempColu ...
- [Linux] Chang DNS Setting on Linux
主机的虚拟机使用 NAT 模式时, NAT的DNS不好用.于是需要将虚拟机的DNS改成和主机的一样,这样虚拟机也可以请求互联网资源. Linux的DNS 服务器定义在 /etc/resolv.conf
- ThinkPHP魔术方法
我们在使用thinkphp开发系统的时候,有时候会用到getById('1')这个方法快速的获取一条信息的内容,比用where(" id =1 ")->find();好用多了 ...