leetcode79
2024-09-26 18:14:46
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
最新文章
- Linux_scp命令
- linux(以ubuntu为例)下Android利用ant自动编译、修改配置文件、批量多渠道,打包生成apk文件
- 创建动态组-以OU为单位
- CAS原理
- Ring3下Hook NtQueryDirectoryFile隐藏文件
- Windows服务程序的原理及实现(服务分为WIN32服务和系统服务)
- 职业选择測试(A/B卷)
- 文件I/O的操作实例
- NDK调试
- vxWorks应用程序加载的另一种办法
- Mybatis 源码之Plugin类解析
- solr研磨之facet
- 博客 新址: https://pheromone.github.io/
- scrapy 爬取智联招聘
- 域名到站点的负载均衡技术一览(主要是探讨一台Nginx抵御大并发的解决方案)(转)https://www.cnblogs.com/EasonJim/p/7823410.html
- 获取微信的access_tokey,处理json格式的数据
- 【读书笔记】iOS-storyBoard-为一个按钮添加一个点击事件
- ionic访问odoo 11接口
- CentOS安装PHP7+Nginx+MySQL
- 【HNOI2018】游戏
热门文章
- jmeter操作练习
- @Autowired Map<;String , Object>; xx
- 《ProgrammingHive》阅读笔记-第二章
- idea快捷键的设置
- Go Example--defer
- 浅谈一下mshta在CVE-2017-11882里的命令构造
- Android studio 启动模拟器出现 VT-x is disabled in BIOS 以及 /dev/kvm is not found
- Python Django orm操作数据库笔记之QuerySet API
- Dockerfile之nginx(六)
- Transaction rolled back because it has been marked as rollback-only 原因 和解决方案