leetCode 79.Word Search (词搜索) 解题思路和方法

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 =

[
  ["ABCE"],
  ["SFCS"],
  ["ADEE"]
]

word = "ABCCED",
-> returns true,

word = "SEE",
-> returns true,

word = "ABCB",
-> returns false.

思路:此题意思是下个字符必须与上个字符相邻,且不能重复使用。代码上难度不大,最主要的是控制搜索的方向,分别向四个方向搜索即可。

代码如下:

public class Solution {
    boolean[][] b;
    public boolean exist(char[][] board, String word) {
        if(word.length() == 0){
            return true;
        }

        for(int i = 0; i < board.length;i++){
            for(int j = 0; j < board[0].length; j++){
                if(board[i][j] == word.charAt(0)){
                    b = new boolean[board.length][board[0].length];
                    if(search(board,i,j,b,0,word)){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    //根据首字母的i,j位置查找word
    private boolean search(char[][] board,int i,int j,boolean[][] b,int index,String word){

        if(board[i][j] != word.charAt(index)){//字符不相等。返回false
            return false;
        }
        if(index == word.length() - 1){//如果到达最后一个,返回true
        	return true;
        }

        b[i][j] = true;//标记已使用

        if(i > 0 && !b[i-1][j] && search(board,i-1,j,b,index+1,word)){//如果i>0,且i-1的值没有被使用,搜索
            return true;
        }

        if(i < board.length-1 && !b[i+1][j] && search(board,i+1,j,b,index+1,word)){//如果没有边界,且i+1的值没有被使用,搜索
            return true;
        }

        if(j > 0 && !b[i][j-1] && search(board,i,j-1,b,index+1,word)){//如果j>0,且j-1的值没有被使用,搜索
            return true;
        }

        if(j < board[0].length-1 && !b[i][j+1] && search(board,i,j+1,b,index+1,word)){//如果没到边界,且j+1的值没有被使用,搜索
            return true;
        }
        b[i][j] = false;
        return false;
    }

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-11 18:03:18

leetCode 79.Word Search (词搜索) 解题思路和方法的相关文章

LeetCode 79. Word Search单词搜索 (C++)

题目: 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 b

(Java) 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 letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be us

LeetCode 0079. Word Search单词搜索【Python】

LeetCode 0079. Word Search单词搜索[Medium][Python][DFS] Problem LeetCode 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 horiz

[LeetCode OJ] Word Search 深度优先搜索DFS

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 us

[C++]LeetCode: 97 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 b

leetcode 79 Word Search ----- java

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 us

leetCode 39.Combination Sum(组合总和) 解题思路和方法

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includi

leetCode 75.Sort Colors (颜色排序) 解题思路和方法

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

leetCode 49.Anagrams (回文构词法) 解题思路和方法

Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 思路:这题要是解,必须知道什么是回文构词法.所谓回文构词法就是把一个单词的顺序调整,形成新的单词,如"eat","tea"就是回文构词法. 所以回文构词法一定是相同的字母不同的顺序,而且最少有两个单词. 本题是将单词排序之