【Leetcode】79. Word Search

思路:

遍历矩阵,结合dfs解即可。

#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:

    Solution() {}

    bool exist(vector<vector<char>>& board, string word)
    {
        //初始化都没有被访问过
        this->rowCount = board.size();
        if (rowCount == 0) {
            return false;
        }
        this->colCount = board[0].size();
        for (int i = 0; i < rowCount; i++) {
            vector<int> v(colCount, 0);
            visit.push_back(v);
        }

        this->word = word;
        this->board = board;

        for (int i = 0; i < rowCount; i++){
            for (int j = 0; j< colCount; j++) {
                if (board[i][j] == word[0] && dfs(i, j, 0)) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     *@param row 当前字符所在行
     *@param col 当前字符所在列
     *@param index word当前被扫描的位置
     */
    bool dfs(int row, int col, int index)
    {
        if (index == word.size() - 1) {
            return true;
        }
        //当前位置标志为被访问
        visit[row][col] = 1;
        //up
        if (row - 1 >= 0 && !visit[row - 1][col] && board[row - 1][col] == word[index + 1] ) {
            if (dfs(row - 1, col, index + 1)) {
                return true;
            }
        }
        //down
        if (row + 1 < this->rowCount && !visit[row + 1][col] && board[row + 1][col] == word[index + 1]) {
            if (dfs(row + 1, col, index + 1)) {
                return true;
            }
        }
        //left
        if (col - 1 >= 0 && !visit[row][col - 1] && board[row][col - 1] == word[index + 1]) {
            if (dfs(row, col - 1, index + 1)) {
                return true;
            }
        }
        //right
        if (col + 1 < this->colCount && !visit[row][col + 1] && board[row][col + 1] == word[index + 1]) {
            if (dfs(row, col + 1, index + 1)) {
                return true;
            }
        }
        //不满足,置为0
        visit[row][col] = 0;
        return false;
    }

private:
    vector<vector<char>> board;
    string word;
    vector<vector<int>> visit;
    int rowCount;
    int colCount;
};

int main(int argc, char *argv[])
{
    vector<vector<char>> board =  {
            {‘a‘,‘b‘}};
    Solution s;
    string word = "ba";
    bool ret = s.exist(board, word);
    cout<<ret<<endl;
    return 0;
}

  

时间: 2024-12-23 21:37:36

【Leetcode】79. Word Search的相关文章

【LeetCode】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【LeetCode】Validate Binary Search Tree 解题报告

今天CSDN博客发生异常,折腾了大半天终于发出了这篇博文. [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a

【LeetCode】Recover Binary Search Tree 解题报告

[题目] Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? [解析] 题意:二叉搜索树中,有两个结点的位置

【LeetCode】二叉查找树 binary search tree(共14题)

链接:https://leetcode.com/tag/binary-search-tree/ p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [220]Contains Duplicate III [315]Count of Smaller Numbers After Self [327]Count of Range Sum [352]Data Stream as Disjoint Intervals [493]

【leetcode】 Unique Binary Search Trees (middle)☆

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 找数字连续最大乘积子序列. 思路:这个麻烦在有负数和0,我的方法,如果有0,一切都设

【LeetCode】 Recover Binary Search Tree BST 中序遍历

题目:Recover Binary Search Tree <span style="font-size:18px;">/* * LeetCode: recover the binary search tree * 题目:二叉树中有两个节点被交换了位置,找出它们,并且将它们换回来,要求用o(n)的连续空间 * 知识点:1.BST树的特点:中序遍历后的节点的排列是按照非降的顺序 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两

【Leetcode】Unique Binary Search Trees II

题目链接:https://leetcode.com/problems/unique-binary-search-trees-ii/ 题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below.

【LeetCode】Unique Binary Search Trees II 不同的二叉查找树II

今早起来做 LeetCode,结果被这道题卡了将近1个半小时,忍着没有去搜答案,最后还是被我想出来了,而且直接一次AC,哈哈!现在在这里记录一下解题思路. 原题: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's sh

【LeetCode】Validate Binary Search Tree 二叉查找树的判断

题目: Given a binary tree, determine if it is a valid binary search tree (BST). 知识点:BST的特点: 1.一个节点的左子树的所有点都小于或等于这个点的值,右子树的所有节点的值大于该节点的值: 2.最左节点值最小,最右节点值最大: 3.中序遍历结果值是一个非降的数列 问题:如果用Integer.MAX_VALUE会过不了测试用例,可是按照TreeNode中的定义,val值也是int呀,没办法用了Long,如果谁知道,麻烦