【LeetCode】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.

For example,

Given board =

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

word = "ABCCED",
-> returns true,

word = "SEE",
-> returns true,

word = "ABCB",
-> returns false.

【解析】

DFS;回溯;递归。

public class Solution {
    char[][] board;
    int m, n;

    public boolean exist(char[][] board, String word) {
        if (board == null || word == null) return false;
        if (("").equals(word)) return true;

        this.board = board;
        m = board.length;
        n = board[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == word.charAt(0)) {
                    char tmp = board[i][j];
                    board[i][j] = '#';
                    if (dfs(i, j, word.substring(1))) return true;
                    board[i][j] = tmp;
                }
            }
        }
        return false;
    }

    public boolean dfs(int i, int j,  String word) {
        if (("").equals(word)) return true;
        char tar = word.charAt(0);
        String left = word.substring(1);
        if (i > 0 && board[i - 1][j] == tar) {
            board[i - 1][j] = '#';
            if (dfs(i - 1, j, left)) return true;
            board[i - 1][j] = tar;
        }
        if (j > 0 && board[i][j - 1] == tar) {
            board[i][j - 1] = '#';
            if (dfs(i, j - 1,left)) return true;
            board[i][j - 1] = tar;
        }
        if (i < m - 1 && board[i + 1][j] == tar) {
            board[i + 1][j] = '#';
            if (dfs(i + 1, j, left)) return true;
            board[i + 1][j] = tar;
        }
        if (j < n - 1 && board[i][j + 1] == tar) {
            board[i][j + 1] = '#';
            if (dfs(i, j + 1, left)) return true;
            board[i][j + 1] = tar;
        }
        return false;
    }
}
时间: 2025-01-04 05:15:47

【LeetCode】Word Search 解题报告的相关文章

LeetCode: Word Search 解题报告

Word SearchGiven 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 ma

[LeetCode] Word Search [37]

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

LeetCode: Word Search [079]

[题目] 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

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution 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

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

[LeetCode] Word Search II

A simple combination of Implement Trie (Prefix Tree) and Word Search. If you've solved them, this problem will become easy :-) The following code is based on DFS and should be self-explanatory enough. Well, just go ahead and read it. It is long but c

LeetCode ZigZag Conversion 解题报告

对输入字符串,做蛇形变化,然后按行输出. https://oj.leetcode.com/problems/zigzag-conversion/ 例如:The string "PAYPALISHIRING"  的蛇形变化如下: P        A           H        N A   P   L    S     I     I   G Y         I            R 最后要求输出的就是:"PAHNAPLSIIGYIR" Write

[Leetcode] 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