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的更多相关文章

  1. 【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 ...

  2. 【leetcode】Word Search (middle)

    今天开始,回溯法强化阶段. Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...

  3. 【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 ...

  4. 【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 ...

  5. 【数组】word search

    题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...

  6. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  7. 【CF528D】Fuzzy Search(FFT)

    [CF528D]Fuzzy Search(FFT) 题面 给定两个只含有\(A,T,G,C\)的\(DNA\)序列 定义一个字符\(c\)可以被匹配为:它对齐的字符,在距离\(K\)以内,存在一个字符 ...

  8. 【计算机视觉】Selective Search for Object Recognition论文阅读3

    Selective Search for Object Recoginition surgewong@gmail.com http://blog.csdn.net/surgewong       在前 ...

  9. 【HDU2222】Keywords Search(AC自动机)

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

随机推荐

  1. IDEA重新打jar包时报错MANIFEST.MF already exists in VFS

    报错原因:曾经打过jar包了,把之前的包删掉无用,VFS:虚拟文件系统.即使删掉之前的包,信息依然会在此处.故删掉MANIFEST文件夹,重新打包即可解决.

  2. spring-mvc 的一些使用技巧(转)

    APP 服务端的 Token 验证 通过拦截器对使用了@Authorization注解的方法进行请求拦截,从 http header 中取出 token 信息,验证其是否合法.非法直接返回 401 错 ...

  3. @RequestParam,@PathVariable,@ResponseBody,@RequestBody,@ModelAttribute学习

    1.@RequestParam使用于参数上,用于将请求参数映射到指定参数变量上 例如: @RequestMapping(value="/hello",method=RequestM ...

  4. WebLogic 11g重置用户密码

    weblogic安装后,很久不用,忘记访问控制台的用户名或者密码,可通过以下步骤来重置用户名密码. 版本:WebLogic Server 11g 说明:%DOMAIN_HOME%:指WebLogic ...

  5. Examples osgparticleshader例子学习

    Examples osgparticleshader  粒子与shader的使用 参考文档 http://blog.csdn.net/csxiaoshui/article/details/234345 ...

  6. intent 支持的action 动作

    String ACTION_AIRPLANE_MODE_CHANGED Broadcast Action: The user has switched the phone into or out of ...

  7. MySQL插入数据性能调优

    插入数据性能调优总结: 1.SQL插入语句调优 2.如果是InnoDB引擎的话,尝试开启事务,批量提交 3.调整MySQl数据库配置     参考: 百度空间 - MySQL插入数据性能调优 CSDN ...

  8. 利用泛型和反射,管理配置文件,把Model转换成数据行,并把数据行转换成Model

    利用泛型和反射,管理配置文件,把Model转换成数据行,并把数据行转换成Model   使用场景:网站配置项目,为了便于管理,网站有几个Model类来管理配置文件, 比如ConfigWebsiteMo ...

  9. man screen

    http://www.gnu.org/software/screen/manual/screen.html Screen User's Manual Next: Overview, Previous: ...

  10. 如何将安卓手机中已安装的应用导出一个apk安装包

    首先安装ES文件浏览器 然后应用里找到你要导出apk的应用,长按应用,备份应用, 然后在upbacks/apps文件夹里找吧 不得不说 ES浏览器还是屌啊