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;
}
}

最新文章

  1. JavaScript继承的模拟实现
  2. JavaScript 函数节流和函数去抖应用场景辨析
  3. web app开发之rem
  4. Greenplum迁移到配置不同的GP系统
  5. 摄像头(1)拍照的主要API,权限和特性,判断有没有摄像头的方法
  6. java 线程学习
  7. 根据 中序遍历 和 后序遍历构造树(Presentation)(C++)
  8. 【论文速读】Yuliang Liu_2017_Detecting Curve Text in the Wild_New Dataset and New Solution
  9. vim简单命令
  10. git版本管理规范
  11. JAVABEAN递归转MAP实现
  12. php的2种域名转向写法
  13. 关于ajax返回数据处理
  14. 获奖感想和Java学习总结
  15. IO密集型 计算密集型
  16. POJ-3078.Shuffle&#39;m Up(简单模拟题)
  17. NFS 网络文件系统测试笔记
  18. 用大白话揭开Ajax长轮询(long polling)的神秘面纱
  19. 【BZOJ】1617: [Usaco2008 Mar]River Crossing渡河问题(dp)
  20. js i++ 与 ++i 的区别

热门文章

  1. Java 异常处理的优劣
  2. Java 基础【02】 Super 用法
  3. 初级Springboot(一)
  4. 洛谷——P1588 丢失的牛
  5. LightOj 1215 Finding LCM
  6. Spring Boot中使用AOP记录请求日志
  7. CSS 发明者 H&#229;kon Wium Lie 访谈--csdn zhangxin09
  8. 浮窗WindowManager view返回和Home按键事件监听
  9. hdu5384
  10. lfu-cache(需要O(1),所以挺难的)