zoukankan      html  css  js  c++  java
  • 【剑指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;
        }
    };
    
  • 相关阅读:
    overflow妙用--去除默认滚动条,内容仍可滚动
    call()与构造函数的运用
    this与super
    构造方法
    多态
    抽象类与接口
    面向对象的基本特征
    类与对象
    面向过程与面向对象
    java自动拆装箱
  • 原文地址:https://www.cnblogs.com/galaxy-hao/p/12329118.html
Copyright © 2011-2022 走看看