【WordSearch】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.
public class Solution {
public boolean exist(char[][] board, String word) {
if(word.length()==0)
return true;
if(board.length==0)
return false;
boolean[][] bol = new boolean[board.length][board[0].length];
for(int i=0;i<board.length;i++){
for(int j=0;j<board[i].length;j++){
if(board[i][j]==word.charAt(0)&&findExist(i,j,0,board,bol,word)){
return true;
}
}
}
return false;
}
private boolean findExist(int x, int y,int wa, char[][] board, boolean[][] bol, String word) {
if(wa>=word.length())
return true;
if(x<0||y<0)
return false;
if(x>=board.length||y>=board[x].length)
return false;
if(bol[x][y])
return false;
if(board[x][y]!=word.charAt(wa))
return false;
bol[x][y]=true;
boolean re =findExist(x+1,y,wa+1,board,bol,word)||findExist(x,y+1,wa+1,board,bol,word)||
findExist(x-1,y,wa+1,board,bol,word)||findExist(x,y-1,wa+1,board,bol,word);
bol[x][y]=false;
return re;
}
}
【WordSearch】Word Search的更多相关文章
- 【leetcode】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】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】【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 ...
- 【数组】word search
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- 【CF528D】Fuzzy Search(FFT)
[CF528D]Fuzzy Search(FFT) 题面 给定两个只含有\(A,T,G,C\)的\(DNA\)序列 定义一个字符\(c\)可以被匹配为:它对齐的字符,在距离\(K\)以内,存在一个字符 ...
- 【计算机视觉】Selective Search for Object Recognition论文阅读3
Selective Search for Object Recoginition surgewong@gmail.com http://blog.csdn.net/surgewong 在前 ...
- 【HDU2222】Keywords Search(AC自动机)
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
随机推荐
- es6 String.raw()
模板字符串可以是原始的: ES6还为原生的String对象,提供了一个raw方法. 若使用String.raw 作为模板字符串的前缀,则模板字符串可以是原始(raw)的.反斜线也不再是特殊字符,\n ...
- 牛客网 牛客小白月赛1 A.简单题-控制输出格式setiosflags()函数+setprecision()函数
水一水博客,都不好意思写这篇博客,毕竟已经不是大一的了. 难得能把一整套题都写出来(日常智障).但是在这里不写G题あなたの蛙は旅⽴っています的题解. 有毒,G题关了流同步只能过94%的样例,说我运行超 ...
- python--文本处理1
1.字符和字符值之间的转换 内建函数:ord(),chr() >>> print ord("a") 97 >>> print chr(97) a ...
- 词典对象(NSDictionary和NSMutableDictionary)
词典(dictionary)顾名思义就是同由键-值组成的数据集合.与在词典中查找单词定义一样,可以通过对象的键从词典中获取需要的对象,看到 这里,你是不是想起了java中的map?和NSArray一样 ...
- vscode格式化代码无效--可能的解决方法
因为vscode默认启用了根据文件类型自动设置tabsize的选项,因此,可以通过关闭自动设置选项,防止格式覆盖.在用户设置里添加如下配置即可: "editor.detectIndentat ...
- 从头写一个Cucumber测试(一) Selenium Test
转载:https://yaowenjie.github.io/%E7%BC%96%E7%A8%8B%E7%9B%B8%E5%85%B3/cucumber-test, 背景(废话不读系列) 前段时间 ...
- 百科知识 DCR文件如何打开
使用IE可以打开,但是需要先安装Adobe Shockwave Player 天空软件下载地址:http://fpdownload.macromedia.com/get/shockwave/defau ...
- 编写自己的UDTF
1. UDTF介绍 UDTF(User-Defined Table-Generating Functions) 用来解决 输入一行输出多行(On-to-many maping) 的需求. 2. 编写自 ...
- C#中的 SET ,GET
C#中get和SET,看来看去还是看不懂,通俗一点解释一下,用了有什么好处,不用会怎么样如果你这样写是没有什么不一样的. private int __Old; public int Old{ get{ ...
- 函数式编程( Functional)与命令式编程( Imperative)对比
1.函数式编程带来的好处 函数式编程近些年异军突起,又重新回到了人们的视线,并得到蓬勃发展.总结起来,无外乎如下好处: 1.减少了可变量(Immutable Variable)的声明,程序更为安全. ...