(每日算法)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.

//如何构造辅助函数的参数?
//逐个字母匹配的时候,如何传递下一个要匹配的字母?---通过索引参数的形式
//走过的路要标记,不管可行不可行,都要重新标记?
//边界条件的考虑?
class Solution {
public:
    bool exist(vector<vector<char>> &board, string word) {
        const int m = board.size();
        const int n = board[0].size();
        vector<vector<bool>> visited(m, vector<bool>(n, false));//将访问标记数组置空
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                if(dfs(board, word, 0, i, j, visited))//单词可在任意位置开始匹配
                    return true;                      //只要有一个位置完全匹配即匹配
        return false;
    }
    static bool dfs(vector<vector<char>> &board, string word, int index,
                    int x, int y, vector<vector<bool>>& visited)//辅助函数,自定义参数
    {
        if(index == word.size())    //单词大小和索引相等即匹配
                                    //当单词为空的时候是满足的
                                    //下一个要查找的索引和单词大小相等说明,
                                    //word的0 - index的位置的字母已经匹配
            return true;
        if(x < 0 || y < 0 || x >= board.size() || y >= board[0].size()) //不可越界
            return false;
        if(visited[x][y]) //如果之前访问过,那么直接返回false
            return false;
        if(board[x][y] != word[index]) //不相等,这条路走不通,剪枝
            return false;
        visited[x][y] = true; //先标记为走过,因为下一次会走向四个方向
        bool ret = dfs(board, word, index + 1, x, y + 1, visited) ||
                dfs(board, word, index + 1, x, y - 1, visited)    ||
                dfs(board, word, index + 1, x - 1, y, visited)     ||
                dfs(board, word, index + 1, x + 1, y, visited);
        visited[x][y] = false;  //走过之后,不过不匹配,要还原为没有走过
        return ret;
    }
};
				
时间: 2025-01-04 15:53:13

(每日算法)LeetCode --- Word Search(矩阵中查找单词)的相关文章

[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

杨氏矩阵中查找元素

在杨氏矩阵中查找一个元素是否存在 杨氏矩阵即每一行均以递增顺序排列,每列从上到下也为递增顺序 方法一:数组 #include<stdio.h> #include<stdlib.h> #define COLS 3 #define ROWS 3 //要查找只要在找到右上角的元素和输入元素进行比较.如果右上角元素大,即可排除其他行,若小 //,则可排除本行,继续循环,用输入元素和右上角的元素进行比较 int find(int arr[][COLS], int rows, int cols

[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

(每日算法)LeetCode--Set Matrix Zeroes (矩阵置零)

给定一个矩阵,如果有零元素那么就将零元素所在的行和列都置为零. Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 最直观的解法就是开辟一个新的矩阵,当原矩阵存在零元素的时候,就将新矩阵的对应行和列置为零.这样空间复杂度较高,也是题目不允许的. 题目的难点就在于,如果遇到零元素之后马上在矩阵上操作,将所在的行和列都置为零.在接下来的遍历中,如果你再遇到零,你讲不

在杨氏矩阵中查找一个数字是否存在

杨氏矩阵 有一个二维数组. 数组的每行从左到右是递增的,每列从上到下是递增的. 在这样的数组中查找一个数字是否存在. 时间复杂度小于O(N); 数组: 1  2  3    1  3  4    1  2  3 2  3  4    2  4  5    4  5  6 3  4  5    4  5  6    7  8  9 1 #include<stdio.h> 2 3 #define ROW 3 4 #define COL 3 5 6 int Find_num(int arr[ROW]

python3在word文档中查找多行文字是否存在

工作中碰到这样一个情况:有多个关键词存在文本文档txt中,想查找下在某个较大的word文档中,这些关键词是否都含有,没有关键词的显示出来. 因为关键词比较多,并且这个工作还是经常会有的,这个情况我试着用Python3写代码解决. 分析后,需要用到的模块有:docx,另外还有txt文档的读取和字符串的匹配. 首先是安装docx模块  pip install python-docx 具体实现代码如下: import docxpath = "F:\\check\\source.docx" d

[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

(算法)旋转有序数组中查找某个数

题目: 假设有个有序数组在某个位置旋转,得到新的数组,即为旋转有序数组.如:(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 现给定一个这样的数组,在数组中查找某个数.如果找到,返回下标,否则返回-1: 思路: 思路1: 考虑一个旋转有序数组的特点:前面部分是递增的,后面部分也是递增的,即先后两部分都为递增有序数组,因此可以用二分查找来做. 假设数组为A,下标从left到right,查找的数字为target,那么mid=(left+((right