友情支持

如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜

支付宝

微信

有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。

wx jikerizhi

公众号的微信号是: jikerizhi因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。

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.

思路分析

0079 01

0079 02

  • 一刷

  • 二刷

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
 * Runtime: 4 ms, faster than 89.90% of Java online submissions for Word Search.
 *
 * Memory Usage: 38.4 MB, less than 97.96% of Java online submissions for Word Search.
 *
 * Copy from: https://leetcode.com/problems/word-search/discuss/27658/Accepted-very-short-Java-solution.-No-additional-space.[Accepted very short Java solution. No additional space. - LeetCode Discuss]
 *
 * @author D瓜哥 · https://www.diguage.com
 * @since 2020-01-03 17:31
 */
public boolean exist(char[][] board, String word) {
    if (Objects.isNull(board) || board.length == 0) {
        return false;
    }
    if (Objects.isNull(word) || word.length() == 0) {
        return false;
    }
    char[] chars = word.toCharArray();
    for (int y = 0; y < board.length; y++) {
        for (int x = 0; x < board[y].length; x++) {
            if (exist(board, y, x, chars, 0)) {
                return true;
            }
        }
    }
    return false;
}

private boolean exist(char[][] board, int y, int x, char[] word, int i) {
    if (i == word.length) {
        return true;
    }
    if (y < 0 || x < 0 || y == board.length || x == board[y].length) {
        return false;
    }
    if (board[y][x] != word[i]) {
        return false;
    }
    board[y][x] = Character.MAX_VALUE;
    boolean existable = exist(board, y - 1, x, word, i + 1)
            || exist(board, y, x + 1, word, i + 1)
            || exist(board, y + 1, x, word, i + 1)
            || exist(board, y, x - 1, word, i + 1);
    board[y][x] = word[i];
    return existable;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
 * @author D瓜哥 · https://www.diguage.com
 * @since 2024-09-21 21:39:49
 */
public boolean exist(char[][] board, String word) {
  for (int r = 0; r < board.length; r++) {
    for (int c = 0; c < board[r].length; c++) {
      if (board[r][c] == word.charAt(0)) {
        if (backtrack(board, r, c, word, 0)) {
          return true;
        }
      }
    }
  }
  return false;
}

private boolean backtrack(char[][] board, int row, int column,
                          String word, int idx) {
  if (idx >= word.length()) {
    return true;
  }
  if (row < 0 || row >= board.length
    || column < 0 || column >= board[row].length
    || board[row][column] == ' '
    || board[row][column] != word.charAt(idx)) {
    return false;
  }
  board[row][column] = ' ';
  boolean result = backtrack(board, row - 1, column, word, idx + 1)
    || backtrack(board, row + 1, column, word, idx + 1)
    || backtrack(board, row, column - 1, word, idx + 1)
    || backtrack(board, row, column + 1, word, idx + 1);
  board[row][column] = word.charAt(idx);
  return result;
}