【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 ...
随机推荐
- TStringList,快速解析 查找测试。。。很有用,再也不用 FOR 循环了
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABKAAAALHCAIAAAA2Gq0zAAAgAElEQVR4nOydeVgUV76wK5OZb5JJZi
- 使用redis-stat来监控redis实例
https://blog.csdn.net/xiao_jun_0820/article/details/78189576 https://blog.csdn.net/u010022051/articl ...
- git-版本管理工具的介绍+发展史+分布式版本控制系统和集中式版本控制系统的区别
一.版本管理工具的介绍: 1.备份文件: 2.记录历史: 3.多端共享: 4.团队协作: 二.版本管理工具的发展史: 1.cvs: 集中式 1985: 2.svn: 集中式 2000: 3 ...
- HDU 4349 Xiao Ming's Hope 找规律
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/ ...
- IntelliJ IDEA出现:java: Compilation failed: internal java compiler error的问题解决
这两处地方要同时修改成一样的. 参考: http://blog.csdn.net/u011275152/article/details/45242201
- 如何提高NodeJS程序的运行的稳定性
如何提高NodeJS程序运行的稳定性 我们常通过node app.js方式运行nodejs程序,但总有一些异常或错误导致程序运行停止退出.如何保证node程序的稳定运行? 下面是一些可以考虑的方案: ...
- Kermit,Xmodem,1K-Xmodem,Ymodem,Zmodem传输协议小结
来自:http://blog.163.com/czblaze_3333/blog/static/208996228201272295236713/ Kermit协议 报文格式: 1. MA ...
- Headroom.js插件用法
一.Headroom.js是什么? Headroom.js是一个轻量级.高性能的JS小工具(不依赖不论什么工具库.),它能在页面滚动时做出响应. 此页面顶部的导航条就是一个鲜活的案例,当页面向下滚动时 ...
- 关于Label::createWithBMFont中资源文件使用的坑爹问题解决方式
1.问题 使用Label的createWithBMFont,结果.fnt的资源总是找不到或者获取数据失败.原来.fnt资源的使用须要配合该资源的.png共同 使用,如bitmapFontTest3.f ...
- react 调用 function 的写法 及 解决 react onClick 方法自动执行
1.react 调用方法的写法 (1)方式一 onClick={this.getFetchData.bind(this,item.id)} (2)方式二 getFetchData(e){ this.s ...