当前位置:网站首页>Leetcode79. 单词搜索
Leetcode79. 单词搜索
2022-08-09 23:32:00 【Java全栈研发大联盟】
题目传送地址: https://leetcode.cn/problems/word-search/submissions/
运行效率:
代码如下:
//回溯法,递归
public static boolean exist(char[][] board, String word) {
HashSet<String> set = new HashSet<>();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == word.charAt(0)) {
if (recur(i, j, board, word, 0, set)) {
return true;
}
}
}
}
return false;
}
//注意要在recur函数的开头set.add(row + ":" + col); 且在结尾要 set.remove(row + ":" + col); 这才是回溯的状态的还原
public static boolean recur(int row, int col, char[][] board, String word, int k, HashSet<String> set) {
//处理边界情况
if (k == word.length() - 1) {
if (board[row][col] == word.charAt(k)) {
set.add(row + ":" + col);
return true;
}
return false;
}
if (board[row][col] != word.charAt(k)) {
return false;
}
//当前坐标的元素能不能成为贪吃蛇的蛇头, 取决于左、右、下 三个位置的情况
boolean bottom;
boolean left;
boolean right;
boolean top;
set.add(row + ":" + col);
boolean result = false;
//下边相邻的坐标
if (row + 1 < board.length && !set.contains((row + 1) + ":" + col)) {
bottom = recur(row + 1, col, board, word, k + 1, set);
if (bottom) {
result = true;
}
}
//左边相邻的坐标
if (col - 1 >= 0 && !set.contains(row + ":" + (col - 1))) {
left = recur(row, col - 1, board, word, k + 1, set);
if (left) {
result = true;
}
}
//右边相邻的坐标
if (col + 1 < board[0].length && !set.contains(row + ":" + (col + 1))) {
right = recur(row, col + 1, board, word, k + 1, set);
if (right) {
result = true;
}
}
//上边相邻的坐标
if (row - 1 >= 0 && !set.contains((row - 1) + ":" + col)) {
top = recur(row - 1, col, board, word, k + 1, set);
if (top) {
result = true;
}
}
set.remove(row + ":" + col);
return result;
}
边栏推荐
- NotWritableError: The current user does not have write permissions when conda creates a new environment
- 微信小程序获取微信用户步数
- CST Studio Suite 2021软件安装包和安装教程
- 错误提示:Syntax error on token “function”, delete this token
- FreeRTOS任务基础
- Seq2Seq论文阅读笔记
- 【「收藏」Oracle 数据库安装】
- 程序员从佩洛西窜访事件中可以学到什么?
- 下班后用微信处理工作时发病身亡,法院判决:工伤!
- 漫谈缺陷管理的自动化实践方案
猜你喜欢
天猫全网商品详情封装接口
[C language] Address book "Static Memory Version"
Leecode-205. 同构字符串
微信小程序获取微信用户步数
ECCV 2022 | 微软开源TinyViT :搞定小模型的预训练能力
从TRPO到PPO(理论分析与数学证明)
安全知识培训——消防安全
ES6 从入门到精通 # 14:迭代器 Iterator 的用法
The technical aspects of the byte have been passed, and the salary has been negotiated for 20K*13, but the result is still being brushed. I asked the HR why...
数字钱包红海角逐,小程序生态快速引入可助力占领智慧设备入口
随机推荐
NTU General Database-Gbase-8a-Learning-04-Deploying Distributed Clusters
KingbaseGIS Jin Cang database using manual (6.3. Geometric object creation function)
CST Studio Suite 2021 software installation package and installation tutorial
无源晶振负载电容值CL匹配方法及说明
【问题解决】训练和验证准确率很高,但测试准确率很低
防火墙之系统防护
AUTOCAD——形位公差如何标注、CAD打断于点的操作
Linux安装Oracle和postgrepSQL数据库
上交所实时行情文件汇总
第十二,十三章 mysql数据类型,视图的课后练习
源码编译安装LAMP和LNMP
JVM内存和垃圾回收-10.直接内存
[C language] In-depth understanding of pointers and arrays (issue 4)
Digital wallets, red sea ecological rapid introduction of small programs can help capture device entry wisdom
二进制、八进制、十进制、十六进制之间的转换
Eureka protects itself
EL表达式
【渗透工具】浏览器数据导出工具
【集训DAY4】矩形【线段树】
Distributed database problem (3): data consistency