leetcode 79. 单词搜索(Word Search)

目录

  • 题目描述:
  • 示例:
  • 解法:

题目描述:

给定一个二维网格和一个单词,找出该单词是否存在于网格中。

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

示例:

board =
[
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
]

给定 word = "ABCCED", 返回 true.
给定 word = "SEE", 返回 true.
给定 word = "ABCB", 返回 false.

解法:

class Solution {
public:
    bool exist(vector<vector<char>>& board, vector<vector<bool>>& visited, string& word, int i, int j){
        if(word == ""){
            return true;
        }
        int m = board.size();
        int n = board[0].size();
        if(i < 0 || i >= m || j < 0 || j >= n){
            return false;
        }
        char ch = word.back();
        word.pop_back();
        if(board[i][j] == ch && visited[i][j] == false){
            visited[i][j] = true;
            if(exist(board, visited, word, i-1, j)){
                return true;
            }
            if(exist(board, visited, word, i+1, j)){
                return true;
            }
            if(exist(board, visited, word, i, j-1)){
                return true;
            }
            if(exist(board, visited, word, i, j+1)){
                return true;
            }
            visited[i][j] = false;  // to recover the original data
        }
        word.push_back(ch);  // to recover the original data
        return false;
    }

    bool exist(vector<vector<char>>& board, string word) {
        if(word == ""){
            return true;
        }
        int m = board.size();
        if(m == 0){
            return false;
        }
        int n = board[0].size();
        if(n == 0){
            return false;
        }
        string target(word.rbegin(), word.rend());
        vector<vector<bool>> visited(m, vector<bool>(n, false));
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(exist(board, visited, target, i, j)){
                    return true;
                }
            }
        }
        return false;
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10772929.html

时间: 2024-12-19 11:58:12

leetcode 79. 单词搜索(Word Search)的相关文章

LeetCode——79. 单词搜索

给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. 示例: board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] 给定 word = "ABCCED", 返回 true. 给定 word = "SEE", 返回 tru

Leetcode 79. 单词搜索

#include <vector> class Solution { public: int n,m; //标记是否被访问过 vector<vector<bool>> visit; // idx(不包括)之前的都已经被访问过了 bool dfs(int i,int j,vector<vector<char>>& board, int idx, string word) { if(idx == word.size()) return tru

79. 单词搜索-回溯算法(leetcode)

给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. 想法:本题跟我们9021 quiz7-8的类型是一样的,9024也用C写过一次,都是在二维数组里搜索,用回溯算法,今天脑袋有点不清醒,以后多刷几次. 学到的点: 1. 每一步递归中,都要注意判断 start_x,start_y的取值范围 2. 学到了python里面的语法还可以直接大于小于判断.

79. 单词搜索

class Solution { int n; int m; int dx[] = new int[] { -1, 0, 1, 0 }; int dy[] = new int[] { 0, -1, 0, 1 }; public boolean exist(char[][] board, String word) { if(board.length==0||board[0].length==0)return false; n = board.length; m = board[0].length;

LeetCode第[79]题(Java):Word Search(矩阵单词搜索)

题目:矩阵单词搜索 难度:Medium 题目内容: 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

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:Add and Search Word - Data structure design(增加以及搜索单词)

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

[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

[Swift]LeetCode212. 单词搜索 II | 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