[Leetcode][第79题][JAVA][单词搜索][DFS][回溯]
【问题描述】[中等]
【解答思路】
1. DFS繁琐版本
class Solution {public boolean exist(char[][] board, String word) {boolean flag = false;int row= board.length;int col= board[0].length;boolean[][] used = new boolean[row][col];for (int i = 0; i <row ; i++) {for (int j = 0; j <col ; j++) {flag = dfs(board,word,used,i,j,0);if(flag){return true;}}}return false;}private boolean dfs(char[][] board, String word,boolean[][] used,int x,int y ,int start) {int[] dx ={0,1,0,-1};int[] dy ={1,0,-1,0};boolean flag = false;int row= board.length;int col= board[0].length;if (start == word.length() - 1) {return board[x][y] == word.charAt(start);}if(board[x][y] == word.charAt(start)){used[x][y] =true;for (int i = 0; i <4 ; i++) {int newx = x+dx[i];int newy = y+dy[i];if(inArea(newx,newy,row,col) && !used[newx][newy]){flag = dfs(board,word,used,newx,newy,start+1);if(flag){return true;}}}used[x][y] =false;}return false;}private boolean inArea(int x,int y,int row,int col){return x>=0 && y>=0 && x<row && y<col;}
}
2. DFS优化版本
public class Solution {private boolean[][] marked;// x-1,y// x,y-1 x,y x,y+1// x+1,yprivate int[][] direction = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};// 盘面上有多少行private int m;// 盘面上有多少列private int n;private String word;private char[][] board;public boolean exist(char[][] board, String word) {m = board.length;if (m == 0) {return false;}n = board[0].length;marked = new boolean[m][n];this.word = word;this.board = board;for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (dfs(i, j, 0)) {return true;}}}return false;}private boolean dfs(int i, int j, int start) {if (start == word.length() - 1) {return board[i][j] == word.charAt(start);}if (board[i][j] == word.charAt(start)) {marked[i][j] = true;for (int k = 0; k < 4; k++) {int newX = i + direction[k][0];int newY = j + direction[k][1];if (inArea(newX, newY) && !marked[newX][newY]) {if (dfs(newX, newY, start + 1)) {return true;}}}marked[i][j] = false;}return false;}private boolean inArea(int x, int y) {return x >= 0 && x < m && y >= 0 && y < n;}public static void main(String[] args) {// char[][] board =
// {// {'A', 'B', 'C', 'E'},
// {'S', 'F', 'C', 'S'},
// {'A', 'D', 'E', 'E'}
// };
//
// String word = "ABCCED";char[][] board = {{'a', 'b'}};String word = "ba";Solution solution = new Solution();boolean exist = solution.exist(board, word);System.out.println(exist);}
}作者:liweiwei1419
链接:https://leetcode-cn.com/problems/word-search/solution/zai-er-wei-ping-mian-shang-shi-yong-hui-su-fa-pyth/
【总结】
1. 一开始没审题 以后是不回头的遍历 审题!!!
2.往往空间优先于时间
3.回溯算法相关题目
[Leedcode][JAVA][第46题][全排列][回溯算法]
[Leetcode][第81题][JAVA][N皇后问题][回溯算法]
[Leetcode][第60题][JAVA][第k个排列][回溯][DFS][剪枝]
[Leetcode][第39题][JAVA][组合总和][回溯][dfs][剪枝]
转载链接:https://leetcode-cn.com/problems/word-search/solution/zai-er-wei-ping-mian-shang-shi-yong-hui-su-fa-pyth/
[Leetcode][第79题][JAVA][单词搜索][DFS][回溯]相关推荐
- [Leetcode][第78题][JAVA][子集][位运算][回溯]
[问题描述][中等] [解答思路] 1. 位运算 复杂度 class Solution {List<Integer> t = new ArrayList<Integer>(); ...
- [Leetcode][第40题][JAVA][数组总和2][回溯][剪枝]
[问题描述][中等] [解答思路] 1. 减法 import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Ar ...
- [Leetcode][第216题][JAVA][数组之和3][回溯]
[问题描述][中等] [解答思路] 回溯 剪树枝 当和超过n 或 个数超过k 1. 正向求和 优化前 class Solution {public List<List<Integer> ...
- [Leetcode][第17题][JAVA][电话号码的字母组合][回溯]
[问题描述][中等] [解答思路] 用哈希表/数组存储每个数字对应的所有可能的字母,然后进行回溯操作. 回溯过程中维护一个字符串,表示已有的字母排列(如果未遍历完电话号码的所有数字,则已有的字母排列是 ...
- [Leetcode][第133题][JAVA][克隆图][DFS][BFS][深拷贝]
[问题描述][中等] [解答思路] 其实就是深拷贝的一个实现,深拷贝就是对于所有的指针成员,不能仅仅是赋值,还有重新分配空间. 深拷贝反应在本题中就是,所有的结点需要重新new出来,而不是直接赋值. ...
- [Leetcode][第174题][JAVA][地下城游戏][DFS][动态规划]
[问题描述][中等] [解答思路] 1. 回溯(暴力)& 优化 超时,需要优化 public int calculateMinimumHP(int[][] dungeon) {if (dung ...
- [Leetcode][第81题][JAVA][N皇后问题][回溯算法]
[问题描述][困难] [解答思路] 1. 主副对角线列 标记 复杂度 import java.util.ArrayDeque; import java.util.ArrayList; import j ...
- [Leetcode][第679题][JAVA][24点游戏][回溯][暴力]
[问题描述][困难] [解答思路] 回溯 时间复杂度:O(1) 空间复杂度:O(1) class Solution {static final int TARGET = 24;static final ...
- [Leetcode][第39题][JAVA][组合总和][回溯][dfs][剪枝]
[问题描述][中等] [解答思路] 1. 回溯 import java.util.ArrayDeque; import java.util.ArrayList; import java.util.De ...
最新文章
- linux 程序包 permission denied,Linux 执行程序 报错误:Permission denied.
- 中文预训练模型ZEN开源,效果领域内最佳,创新工场港科大出品
- Spring MVC Interceptor Handler InterceptorAdapter HandlerInterceptor示例
- 脚注交叉引用序号不一样_利用Word的尾注来做参考文献的注释编号--解决编号加方括号,交叉引用,去除尾注上访横线等问题...
- 亲密关系沟通-【情感勒索】建立良性沟通
- UnityShader25:在Unity中实现泛光
- Deep Inside Convolutional Networks: Visualising Image Classification Models and Saliency Maps
- MySQL 千万级数据SQL的查询优化30条总结
- linux shell 获取系统当前时间 毫秒
- C# OpenFileDialog 打开文件对话框 打开多文件对话框; 并获取其路径,文件名,扩展名/后缀名
- Redis操作工具类——RedisUtil
- 中文核心期刊目录(2008年最新版)
- 单变量微积分笔记—— 积分方法之换元法总结(简单换元和三角换元)
- SECS/GEM编程
- CentOS8桌面环境打开终端
- [2022 Google开发者大会] 机器学习-TensorFlow会议记录
- 全网最详细的Intel CPU体系结构分析(内核源码)
- 【go-zero】go-zero开发环境 如何聚合所有api? caddy反向代理服务分发 微服务设计api聚合方法 best practice
- 超微服务器硬盘红灯_服务器硬盘亮红灯崩溃怎么办?数据丢失都是怎么找回的...
- 【教程】批量删除B站抽奖动态