• 【剑指Offer】面试题12. 矩阵中的路径


    题目

    请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左、右、上、下移动一格。如果一条路径经过了矩阵的某一格,那么该路径不能再次进入该格子。例如,在下面的3×4的矩阵中包含一条字符串“bfce”的路径(路径中的字母用加粗标出)。

    [["a","b","c","e"],
    ["s","f","c","s"],
    ["a","d","e","e"]]

    但矩阵中不包含字符串“abfb”的路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入这个格子。

    示例 1:

    输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
    输出:true
    

    示例 2:

    输入:board = [["a","b"],["c","d"]], word = "abcd"
    输出:false
    

    提示:

    • 1 <= board.length <= 200
    • 1 <= board[i].length <= 200

    思路

    【LeetCode】79. 单词搜索
    以每个位置开头检查是否存在路径。

    代码

    时间复杂度:O(n * m)
    空间复杂度:O(1)

    class Solution {
    public:
        bool exist(vector<vector<char>>& board, string word) {
            int row = board.size(), col = board[0].size();        
            for (int i = 0; i < row; ++i) {
                for (int j = 0; j < col; ++j) {
                    if (board[i][j] == word[0] && dfs(board, i, j, 0, word)) {
                        return true;
                    }                
                }
            }
            return false;
        }
    
        bool dfs(vector<vector<char>> &board, int i, int j, int len, string word) {     
            int row = board.size(), col = board[0].size();
            if (i < 0 || i >= row || j < 0 || j >= col || word[len] != board[i][j]) return false;      
            if (len == word.size() - 1) return true;
            ++len;
            char ch = board[i][j];
            board[i][j] = '#';
            bool ret = dfs(board, i - 1, j, len, word) ||
                       dfs(board, i + 1, j, len, word) ||
                       dfs(board, i, j - 1, len, word) ||
                       dfs(board, i, j + 1, len, word);        
            board[i][j] = ch; //回溯
            return ret;
        }
    };
    

    另一种写法

    使用访问数组表示每个位置是否访问。
    时间复杂度:O(n * m)
    空间复杂度:O(n * m)

    class Solution {
    public:
        bool exist(vector<vector<char>>& board, string word) {
            int row = board.size(), col = board[0].size();   
            vector<vector<bool>> visited(row, vector<bool>(col));
            for (int i = 0; i < row; ++i) {
                for (int j = 0; j < col; ++j) {
                    if (board[i][j] == word[0] && dfs(board, i, j, 0, word, visited)) {
                        return true;
                    }                
                }
            }
            return false;
        }
    
        bool dfs(vector<vector<char>> &board, int i, int j, int len, string word, vector<vector<bool>> &visited) {     
            int row = board.size(), col = board[0].size();
            if (i < 0 || i >= row || j < 0 || j >= col || visited[i][j] || word[len] != board[i][j]) return false;      
            if (len == word.size() - 1) return true;
            ++len;
            visited[i][j] = true;
            bool ret = dfs(board, i - 1, j, len, word, visited) ||
                       dfs(board, i + 1, j, len, word, visited) ||
                       dfs(board, i, j - 1, len, word, visited) ||
                       dfs(board, i, j + 1, len, word, visited);        
            visited[i][j] = false; //回溯
            return ret;
        }
    };
    
  • 相关阅读:
    Xamarin.Forms教程下载安装Xamarin.iOS
    【基于STM32F407IGT6】STemWin5.20d + uCOS-III + FatFS程序下载
    EnOcean-自获能无线电技术
    FreeRTOS 使用指南(转)
    FreeRTOS的内存管理
    FreeRTOS初步认识
    Freertos之系统配置
    FreeRTOS代码剖析
    STM32之FreeRTOS
    STM32 电机控制库的资源
  • 原文地址:https://www.cnblogs.com/galaxy-hao/p/12329118.html
Copyright © 2020-2023  润新知