[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 ...
随机推荐
- 使用GitHub+Hexo搭建个人博客
title: CozyMo date: 2019-12-28 16:01:29 tags: 书写 前言:搭建博客要自己打代码吗? 开始动手:搭建博客的步骤 个性化:更换主题!! 写博客:初识 mark ...
- Linux--Centos7开机启动 mysql5.7.19
参考:http://www.cnblogs.com/Anker/p/3551508.html
- python numpy和矩阵
2.numpy数据选取 lst=[[1, 2, 3], [4, 5, 6]] np.array(lst)[:-1] Out[32]: array([[1, 2, 3]]) np.array(lst)[ ...
- 22. docker 数据持久化 Data Volume
1 . 使用场景 在docker 容器被删除的时候 希望数据不丢失 2 . Volume 的使用 * 注意 在 mysql 的 Dockerfile 内 定义了 VOLUME ["var/ ...
- dozer
1.简介 dozer是用来两个对象之间属性转换的工具,有了这个工具之后,我们将一个对象的所有属性值转给另一个对象时,就不需要再去写重复的set和get方法了. 2.如果两个类之间的属性有些属性意思一样 ...
- C#判断两个字符串是否相等的方法 ,还有char赋空值办法。
string str1="Test"; string str2 = "Test"; if (str1==str2) //第一种判断方式 { //第二种判断方式 ...
- Tkinter控件Canvas
网上关于tkinter的canvas组件系统的中文教程很少,英文教程未知.要么是专业的参考文档,没有丰富的实例,要么在不同的论坛,博客平台零零散散存在一些canvas的例子,这给学习canvas带来了 ...
- upstream(负载均衡)
一.什么是负载均衡 负载均衡,顾名思义是指将负载尽量均衡的分摊到多个不同的服务器,以保证服务的可用性和可靠性,提供给客户更好的用户体验: 负载均衡的直接目标就是尽量发挥多个服务单元的整体效能,要实现这 ...
- JS - ES5与ES6面向对象编程
1.面向对象 1.1 两大编程思想 1.2 面向过程编程 POP(Process-oriented programming) 1.3 面向对象编程 OOP (Object Oriented Progr ...
- 题解-------CF372C Watching Fireworks is Fun
传送门 一道有趣的DP 题目大意 城镇中有$n$个位置,有$m$个烟花要放.第$i$个烟花放出的时间记为$t_{i}$,放出的位置记为$a_{i}$.如果烟花放出的时候,你处在位置$x$,那么你将收获 ...