【数组】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.
思路:
用栈记录当前搜索的路径。
栈存放的节点包括4个成员: 字符c, x,y坐标,已遍历方向p。
注意p在回溯法中是非常重要的,用来记录已遍历过的方向(按照上下左右的顺序),不然的话就会出现无限循环的同一节点进栈出栈。
进栈之后的节点置为'*',以免同一节点多次进栈。
出栈之后的节点恢复为word[wind]。
/**
* @param {character[][]} board
* @param {string} word
* @return {boolean}
*/
var exist = function(board, word) {
if(board.length==0){
return false;
}
var m=board.length,n=board[0].length;
if(m*n<word.length){
return false;
} for(var i=0;i<m;i++){
for(var j=0;j<n;j++){
if(board[i][j]==word[0]){
var stack=[];
var node={
c:word[0],
x:i,
y:j,
p:0
};
stack.push(node);
board[i][j]='*';
var wind=1;
if(wind==word.length){
return true;
}
while(stack.length!=0){
var top=stack[stack.length-1]
if(top.p==0){
top.p=1;
if(top.x>0&&board[top.x-1][top.y]==word[wind]){
var node={
c:word[wind],
x:top.x-1,
y:top.y,
p:0
};
stack.push(node);
board[node.x][node.y]='*';
wind++;
if(wind==word.length){
return true;
}
continue;
}
}
if(top.p==1){
top.p=2;
if(top.x<m-1&&board[top.x+1][top.y]==word[wind]){
var node={
c:word[wind],
x:top.x+1,
y:top.y,
p:0
};
stack.push(node);
board[node.x][node.y]='*';
wind++;
if(wind==word.length){
return true;
}
continue;
}
}
if(top.p==2){
top.p=3;
if(top.y>0&&board[top.x][top.y-1]==word[wind]){
var node={
c:word[wind],
x:top.x,
y:top.y-1,
p:0
};
stack.push(node);
board[node.x][node.y]='*';
wind++;
if(wind==word.length){
return true;
}
continue;
}
}
if(top.p==3){
if(top.y<n-1&&board[top.x][top.y+1]==word[wind]){
var node={
c:word[wind],
x:top.x,
y:top.y+1,
p:0
};
stack.push(node);
board[node.x][node.y]='*';
wind++;
if(wind==word.length){
return true;
}
continue;
}
}
board[top.x][top.y]=top.c;
stack.pop();
wind--;
}
}
}
} return false;
};
【数组】word search的更多相关文章
- [LeetCode] Word Search 词语搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- Word Search I & II
Word Search I Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...
- Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- LeetCode: Word Search 解题报告
Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...
- 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 ...
- Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)
Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...
- Leetcode之回溯法专题-79. 单词搜索(Word Search)
Leetcode之回溯法专题-79. 单词搜索(Word Search) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元 ...
- [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 ...
随机推荐
- Template Method Design Pattern in Java
Template Method is a behavioral design pattern and it’s used to create a method stub and deferring s ...
- 18-11-2 Scrum Meeting 5
1. 会议照片 2. 工作记录 - 昨天完成工作 1 把数据导入数据库 2 中译英选择题和英译中选择题的查询接口 - 今日计划工作 1 配置页面 2 实现中译英选择题和英译中选择题的查询接口 3 整理 ...
- 作业二:注册软件github
注册Github
- IDEA 配置SSH2
系统换成了mac os,因为喜欢它的界面体验,同时受不了win下面系统对硬盘的疯狂访问.发现在mac下面,IDEA真的不错,速度上快,并且它的智能提示真的很厉害.但是导入一个myeclipse的ssh ...
- sql查询优化--数字转换字符串字段
SELECT top 1 pt.* FROM t1where id='20180731223014' SELECT top 1 pt.* FROM t1where id='0180731223014 ...
- SQL去除重复记录
SQL去除重复记录 if not object_id('Tempdb..#T') is null drop table #T Go Create table #T([ID] int,[Name ...
- WPF自定义滚动条
我修改了一些地方,部分代码参考了博主 https://www.cnblogs.com/xiaomingg/ <!-- ScrollViewer 滚动条 --> <Style x:Ke ...
- iOS Keychain 跨应用
Keychain 可以用来持久保存一些信息.通常每个应用都有自己的 Keychain Access.但有时你会需要多个应用共用一些信息.这时需要创建 Keychain Access Group. Ke ...
- python网络编程--线程的方法,线程池
一.线程的其他方法(Thread其他属性和方法) ident() 获取线程id Thread实例对象的方法 isAlive() 设置线程名 getName() 返回线程名 setName() 设置线程 ...
- python单引号和双引号的区别
今天在网上爬虫的时候,很奇怪的发现python的字符串既可以用双引号又可以用单引号,于是就上网百度了一下原因. 原来由于字符串中有可能既有双引号又有单引号,例如: 字符串:demo'1'. 这时候就可 ...