[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 ...
随机推荐
- ssh 账号密码登录设置
找到/etc/ssh/sshd_config文件中的 PasswordAuthentication no 改为PasswordAuthentication yes 并保存. 重启ssh服务:sudo ...
- Maven--mirror 和 repository
参考:http://blog.csdn.net/isea533/article/details/22437511 http://www.cnblogs.com/xdouby/p/6502925.h ...
- 超级顽固的流方式读取doc,docx乱码问题
因为工作中需要一个把doc或者docx的office文档内容,需要读取出来,并且也没展示功能.代码中第一考虑可能就是通过读取流方式,结果写了以后,各种乱码,百科的解决方案也是千奇百怪,第一点:可能是文 ...
- 87.QuerySet API使用详解:create方法
create:创建一条数据,并且保存到数据库中,这个方法相当于先用指定的模型创建一个一个对象,然后再调用这个对象的save方法,示例代码如下: from django.db import connec ...
- 201503-1 图像旋转 Java
思路: 观察输入和输出可以发现,第三列输出为第一行,第二列输出为第二行,第一列输出为第三行.循环即可 import java.util.Scanner; //得分80,本题最高需要输入100W次,因为 ...
- 吴裕雄--天生自然ShellX学习笔记:Shell 文件包含
和其他语言一样,Shell 也可以包含外部脚本.这样可以很方便的封装一些公用的代码作为一个独立的文件. Shell 文件包含的语法格式如下: . filename # 注意点号(.)和文件名中间有一空 ...
- 吴裕雄--天生自然深度学习TensorBoard可视化:命名空间
# 1. 不同的命名空间. import tensorflow as tf with tf.variable_scope("foo"): a = tf.get_variable(& ...
- python编程:从入门到实践----第五章>if 语句
一.一个简单示例 假设有一个汽车列表,并想将其每辆汽车的名称打印出来.遇到汽车名‘bmw’,以全大写打印:其他汽车名,首字母大写 cars=['audi','bmw','subaru','toyota ...
- 关于Weblogic的知识点
一.解决Weblogic域创建.启动.进入控制台慢问题 搭建Weblogic 11g和12c环境时发现,安装正常,以默认组件安装,但是创建域的时候特别慢,一般需要几分钟至10分钟,卡在“创建域安全信息 ...
- win10下安装cygwin全过程
简单讲:cygwin就是在windows系统上跑linux和unix的环境,跨平台移植的应用程序移植. 安装步骤: 下载cygwin: 打开官网https://cygwin.com/install.h ...