[LC] 79. Word Search
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.
Example:
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
] Given word = "ABCCED", return true.
Given word = "SEE", return true.
Given word = "ABCB", return false. Time: O(M * N * 4^|Word|)
class Solution {
int row;
int col;
public boolean exist(char[][] board, String word) {
row = board.length;
col = board[0].length;
boolean[][] visited = new boolean[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (helper(board, visited, word, i, j, 0)) {
return true;
}
}
}
return false;
} private boolean helper(char[][] board, boolean[][] visited, String word, int i, int j, int index) {
if (index == word.length()) {
return true;
}
if (i < 0 || i >= row || j < 0 || j >= col) {
return false;
}
if (word.charAt(index) == board[i][j] && !visited[i][j]) {
visited[i][j] = true;
boolean res = helper(board, visited, word, i + 1, j, index + 1) ||
helper(board, visited, word, i - 1, j, index + 1) ||
helper(board, visited, word, i, j + 1, index + 1) ||
helper(board, visited, word, i, j - 1, index + 1);
// need to clean up visited
visited[i][j] = false;
return res;
} return false;
}
}
class Solution {
int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public boolean exist(char[][] board, String word) {
boolean[][] visited = new boolean[board.length][board[0].length];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (dfs(0, word, board, visited, i, j)) {
return true;
}
}
}
return false;
} private boolean dfs(int index, String word, char[][] board, boolean[][] visited, int row, int col) {
if (index == word.length()) {
return true;
}
// need to check edge after base case e.g. [['A']] 'A', is actually out of board
if (row < 0 || row >= board.length || col < 0 || col >= board[0].length || visited[row][col]) {
return false;
}
if (board[row][col] == word.charAt(index)) {
visited[row][col] = true;
for (int[] direction: directions) {
int nxtRow = direction[0] + row;
int nxtCol = direction[1] + col;
if (dfs(index + 1, word, board, visited, nxtRow, nxtCol)) {
return true;
}
}
visited[row][col] = false;
}
return false;
}
}
[LC] 79. Word Search的更多相关文章
- 刷题79. Word Search
一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...
- [LeetCode] 79. Word Search 词语搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- 【LeetCode】79. Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- [LeetCode] 79. Word Search 单词搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- LeetCode OJ 79. Word Search
题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...
- 79. Word Search在字母矩阵中查找单词
[抄题]: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...
- 79. Word Search
使用的别人的思路,用一个二维数组记录每一个位置是否用过,然后通过递归来判断每一个位置是否符合 public class Solution { public boolean exist(char[][] ...
- Leetcode#79 Word Search
原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...
随机推荐
- java链接redis服务器
1.首先你需要下载驱动包jedis.jar确保下载最新驱动包. 2.public class RedisUtil { //服务器IP地址 private static String ADDR = &q ...
- linux常用命令及小知识点
网络跟踪: 1.mtr 2.tractroute 3.ping 下载命令 curl -O /path/xx wget 直接下载,将文件下载至当前目录 2.linux非22端口进行双机互信时候pu ...
- Python笔记_第四篇_高阶编程_进程、线程、协程_3.进程vs线程
1.多任务的实现原理: 通常我们会设计Mater-Workder模式,Master负责分配任务,Worker负责执行任务,因此多任务环境下,通常是一个Master,多个Worker 2.多进程: 主进 ...
- 生成随机数(Random类)和获取用户输入(Scanner类)
生成指定范围内的随机数 Math.random() 生成随机数,随机数在0到1之间,类型是 double. public class randCase { public static void mai ...
- 阿里云-容器服务之集群服务 k8s(Jenkins+gitlab+k8s的devops)- 01
由于docker官方停止更新Swarm,另外swarm在使用期间出现了很多bug,所以阿里云也在2019年7月发布公告:于2019年12月31日起停止技术支持,请您尽快迁移至容器服务Kubernete ...
- Mybatis 使用分页查询亿级数据 性能问题 DB使用ORACLE
一般用到了mybatis框架分页就不用自己写了 直接用RowBounds对象就可以实现,但这个性能确实很低 今天我用到10w级得数据分页查询,到后面几页就迭代了很慢 用于记录 1.10万级数据如下 [ ...
- PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]
题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...
- python使用进程池多进程时,如何打印错误信息
一.说明 1.python进程池进行多进程运行时,如果有错误,该进程会直接跳过,并且不会打印错误信息. 2.如果需要了解到进程内的错误信息,此时就需要通过捕获异常来输出错误信息了. 二.具体方法如下: ...
- npm安装依赖报 npm ERR! code Z_BUF_ERROR npm ERR! errno -5 npm ERR! zlib: unexpected end of file 这个错误解决方案
今天碰到了一个比较奇怪的问题,下载依赖有问题报错 npm ERR! code Z_BUF_ERROR npm ERR! errno -5 npm ERR! zlib: unexpected end o ...
- 一种循环buffer结构
最新数据循环在buffer[H] -> buffer[L] 放置,记录最新放置Index,对外接口获取数据时,进行两次数据拷贝,Index-H ,index-L 拷贝到数组里