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 used more than once.

For example,
Given board =

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

word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

题意:在矩阵中,相邻的元素可以是上、下、左、右的元素。同时要求矩阵中的同一个字符不能重复匹配。

思路:使用DFS遍历矩阵,直到遍历完字符串,说明匹配。但是需要记录矩阵中哪个字符是已经匹配过的。

由于英文字符范围是0~127,因此遍历某个字符后,进行c^=128操作,该字符在后续匹配中就不会再次

匹配到,从而实现标记的效果。在回溯的时候需要将标记的字符还原。

代码如下:

public boolean exist(char[][] board, String word) {
        for(int i=0; i<board.length; i++) {
            for(int j=0; j<board[0].length; j++) {
                if(exist(board, i, j, word, 0))
                    return true;
            }
        }
        return false;
    }

    public boolean exist(char[][] board, int x, int y, String word, int index) {
        if(index == word.length()) return true;
        if(x < 0 || y<0 || x>=board.length || y>=board[0].length || board[x][y] != word.charAt(index))
            return false;

        board[x][y] ^= 128;
        boolean exist = exist(board, x-1, y, word, index+1) ||
                        exist(board, x+1, y, word, index+1) ||
                        exist(board, x, y-1, word, index+1) ||
                        exist(board, x, y+1, word, index+1) ;
        board[x][y] ^= 128;
        return exist;
    }
时间: 2024-10-17 14:17:24

LeetCode-79 Word Search的相关文章

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"]

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

(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 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

[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

Java for LeetCode 212 Word Search II

Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same le

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

刷题79. Word Search

一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问题,就先看正确的写法,下面是回溯法的代码: class Solution { public: int m,n; //左 上 右 下 int dx[4] = {-1,0,1,0}; int dy[4] = {0,1,0,-1}; bool exist(vector<vector<char>&g

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

79. Word Search/212. Word Search II--图的back tracking -- tier tree 待续

79题, 给你一个二维的board, 只能往上下左右四个方向走,为你是否能找到单词. 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. 分析: 算法并不难,