博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Word Search
阅读量:6914 次
发布时间:2019-06-27

本文共 2441 字,大约阅读时间需要 8 分钟。

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.

DFS Backtracing

Time Complexity

O(mn)
Space Complexity
O(n)

思路

Find the first element of word in the board, if it matches, do dfs.

In dfs, there are two base cases, one is successful base, which is the index equals word length, then return true immediately, one is failed base case, which is if(i < 0 || i >= rows || j < 0 || j >= cols|| visited[i][j] == true || board[i][j] != word.charAt(index)) return falseDo dfs for four directions and we need to keep a visited, set visited[i][j] = true , when it matches, but if the whole word doesn't match. It needs to do backtracing to set visited[i][j] = false.

代码

public boolean exist(char[][] board, String word) {    //corner case    if(board == null || board.length == 0 || board[0] == null || board[0].length == 0 || word.length() == 0) return false;    int rows = board.length, cols = board[0].length;    boolean[][] visited = new boolean[rows][cols];    for(int i = 0; i < rows; i++){        for(int j = 0; j < cols; j++){            if(board[i][j] == word.charAt(0)){                if(dfs(board, word, i, j, 0, visited, rows, cols)) return true;            }        }    }    return false;}private boolean dfs(char[][] board, String word, int i, int j, int index, boolean[][] visited, int rows, int cols){    if(index == word.length()) return true;    if(i < 0 || i >= rows || j < 0 || j >= cols|| visited[i][j] == true || board[i][j] != word.charAt(index)) return false;    visited[i][j] = true;    if(dfs(board, word, i - 1, j, index + 1, visited, rows, cols) || dfs(board, word, i + 1, j, index + 1, visited, rows, cols) || dfs(board, word, i, j + 1, index + 1, visited, rows, cols) || dfs(board, word, i, j - 1, index + 1, visited, rows, cols)) return true;    visited[i][j] = false;    return false;}

Follow up

When the efficiency of the method will be very low?

For example, word = "aaaaaab"
The matrix is like
a a a a a
a a a a a
a a a a b
The dfs will go really deep every time, but can't find the word.

When the efficiency of the method will be very high?

For example, word = "xaaaab"
The matrix is like
a a x a a
a a a a a
a a a a b
We can just check the first letter and then decide if it need do dfs.

转载地址:http://xqicl.baihongyu.com/

你可能感兴趣的文章
Android IPC进程间通讯机制
查看>>
无损音乐资源
查看>>
对SpringAop的思考之基于cglib的动态代理
查看>>
Linux5.3双网卡绑定虚拟成一块网卡
查看>>
轻松获取格林尼治Linux时间戳
查看>>
java 执行cmd、shell 、exe 返回结果
查看>>
linux之iptables详解及配置(一)
查看>>
struts2 通过action返回json
查看>>
DHCP
查看>>
Ubuntu 升级错误信息:mount: mounting none on /dev fail...
查看>>
symantec Desktop and Laptop Option 桌面备份
查看>>
Centos7安装Docker私服Harbor
查看>>
struts2 404处理
查看>>
从LiveJournal后台发展看大规模网站性能优化方法
查看>>
apache的工作模式
查看>>
Linux 常用命令
查看>>
python 中的if __name__ == 'main':
查看>>
各网站平台API接口整理
查看>>
以修改字体为例谈Android的listView开发优化
查看>>
addLoadEvent(func) 不管在页面加载完毕执行多少个函数,都应付自如
查看>>