class Solution {
public boolean exist(char[][] board, String word) {
for(int i=0; i<board.length; i++) {
for(int j=0; j<board[0].length; j++) {
if(exist(board, i, j, word, 0))
return true;
}
}
return false;
} public boolean exist(char[][] board, int x, int y, String word, int index) {
if(index == word.length()) return true;
if(x < 0 || y<0 || x>=board.length || y>=board[0].length || board[x][y] != word.charAt(index))
return false; board[x][y] ^= 128;
boolean exist = exist(board, x-1, y, word, index+1) ||
exist(board, x+1, y, word, index+1) ||
exist(board, x, y-1, word, index+1) ||
exist(board, x, y+1, word, index+1) ;
board[x][y] ^= 128;
return exist;
}
}

补充一个python的实现:

 class Solution:
def dfs(self,board,word,index,rows,coloums,visited,i,j):
if index >= len(word):
return True
if i >= rows or i < or j >= coloums or j < or visited[i][j] == or board[i][j] != word[index]:
return False
visited[i][j] =
result = self.dfs(board,word,index+,rows,coloums,visited,i+,j) or self.dfs(board,word,index+,rows,coloums,visited,i-,j) or self.dfs(board,word,index+,rows,coloums,visited,i,j+) or self.dfs(board,word,index+,rows,coloums,visited,i,j-) visited[i][j] = return result def exist(self, board: 'List[List[str]]', word: 'str') -> 'bool':
rows = len(board)
coloums = len(board[])
visited = [[ for a in range(coloums)] for b in range(rows)]
for i in range(rows):
for j in range(coloums):
if self.dfs(board,word,,rows,coloums,visited,i,j):
return True
return False

最新文章

  1. Linux_scp命令
  2. linux(以ubuntu为例)下Android利用ant自动编译、修改配置文件、批量多渠道,打包生成apk文件
  3. 创建动态组-以OU为单位
  4. CAS原理
  5. Ring3下Hook NtQueryDirectoryFile隐藏文件
  6. Windows服务程序的原理及实现(服务分为WIN32服务和系统服务)
  7. 职业选择測试(A/B卷)
  8. 文件I/O的操作实例
  9. NDK调试
  10. vxWorks应用程序加载的另一种办法
  11. Mybatis 源码之Plugin类解析
  12. solr研磨之facet
  13. 博客 新址: https://pheromone.github.io/
  14. scrapy 爬取智联招聘
  15. 域名到站点的负载均衡技术一览(主要是探讨一台Nginx抵御大并发的解决方案)(转)https://www.cnblogs.com/EasonJim/p/7823410.html
  16. 获取微信的access_tokey,处理json格式的数据
  17. 【读书笔记】iOS-storyBoard-为一个按钮添加一个点击事件
  18. ionic访问odoo 11接口
  19. CentOS安装PHP7+Nginx+MySQL
  20. 【HNOI2018】游戏

热门文章

  1. jmeter操作练习
  2. @Autowired Map&lt;String , Object&gt; xx
  3. 《ProgrammingHive》阅读笔记-第二章
  4. idea快捷键的设置
  5. Go Example--defer
  6. 浅谈一下mshta在CVE-2017-11882里的命令构造
  7. Android studio 启动模拟器出现 VT-x is disabled in BIOS 以及 /dev/kvm is not found
  8. Python Django orm操作数据库笔记之QuerySet API
  9. Dockerfile之nginx(六)
  10. Transaction rolled back because it has been marked as rollback-only 原因 和解决方案