Middle-题目108:79. Word Search

原创 2016年05月31日 19:58:45

题目原文:
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.
For example,
Given board =
[
[‘A’,’B’,’C’,’E’],
[‘S’,’F’,’C’,’S’],
[‘A’,’D’,’E’,’E’]
]
word = “ABCCED”, -> returns true,
word = “SEE”, -> returns true,
word = “ABCB”, -> returns false.
题目大意:
给出一个矩阵,和一个单词,判断单词是否能由矩阵中一条路径连接出来。
题目分析:
一个简单的dfs,教科书式的dfs写法即可,搜到字母错误或者搜到数组边缘则退出,否则每次向四个方向搜索,注意这里的dfs是有返回值的,返回该条件下是否有解。
源码:(language:java)

public class Solution {
    public boolean exist(char[][] board, String word) {
        int row = board.length;
        if(row==0)
            return false;
        int 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(word.charAt(0) == board[i][j]) {
                    if(dfs(board, i, j, visited, word, 0))
                        return true;  
                }
            }
        }
        return false;
    }

    private boolean dfs(char[][] board, int i, int j, boolean[][] visited, String word, int index) {
        if(index == word.length())
            return true;
        else if(i<0 || i>board.length-1 || j<0 || j>board[0].length-1 || word.charAt(index)!=board[i][j] || visited[i][j])
            return false;
        else {
            visited[i][j]=true;
            boolean result = dfs(board, i-1, j, visited, word, index+1) || dfs(board, i+1, j, visited, word, index+1) || dfs(board, i, j-1, visited, word, index+1) || dfs(board, i, j+1, visited, word, index+1);
            visited[i][j] = false;
            return result;
        }

    }
}

成绩:
13ms,beats 58.16%,众数14ms,14.58%
cmershen的碎碎念:
本题基本上是一个教科书式的dfs,区别是dfs带有返回值判断是否有解。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

79. Word Search\513. Find Bottom Left Tree Value\110. Balanced Binary Tree

Word Search 题目描述 代码实现79. Word Search题目描述Given a 2D board and a word, find if the word exists in the ...

[leetcode] 79.Word Search

题目: Given a 2D board and a word, find if the word exists in the grid.The word can be constructed fr...

79. Word Search

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro...
  • x_shuck
  • x_shuck
  • 2016年05月23日 00:35
  • 111

LeetCode 79. Word Search

1. 题目描述 Given a 2D board and a word, find if the word exists in the grid. The word can be con...

79. Word Search 回溯算法

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from ...

leetcode || 79、Word Search

problem: Given a 2D board and a word, find if the word exists in the grid. The word can be con...

【 LeetCode】79. Word Search

问题描述https://leetcode.com/problems/word-search/#/descriptionGiven a 2D board and a word, find if the ...

LeetCode-79.Word Search

https://leetcode.com/problems/word-search/ Given a 2D board and a word, find if the word exists...

Leetcode #79 Word Search

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro...

(java)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 co...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目108:79. Word Search
举报原因:
原因补充:

(最多只允许输入30个字)