力扣 79.单词搜索+212.单词搜索II

单词搜索

【题目】

给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

【思路】
在这里插入图片描述

【代码】

class Solution {
public:
    int dir[4][4]={{-1,0},{1,0},{0,-1},{0,1}};//位置坐标
    bool dfs(int x,int y,int index,vector<vector<char>>& board,string &word,vector<vector<bool>>& visited)
    {
        if(index==word.size()-1)
        	return word[index]==board[x][y];
        if(word[index]==board[x][y])
        {
            visited[x][y]=true;
            for(int i=0;i<4;i++)
            {
                int new_x=x+dir[i][0];
                int new_y=y+dir[i][1];
                if(new_x>=0&&new_x<board.size()&&new_y>=0&&new_y<board[0].size()&&!visited[new_x][new_y])
                {
                    if(dfs(new_x,new_y,index+1,board,word,visited))
                     return  true;
                }
               
            }
            visited[x][y]=false;
        }
        return false;
    }
    bool exist(vector<vector<char>>& board, string word) {
        int m=board.size();//行数
        int n=board[0].size();//列数
        vector<vector<bool>> visited(m,vector<bool>(n));
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
            {
                if(dfs(i,j,0,board,word,visited))
                return true;
            }
            return false;
        
    }
};

单词搜索||

【题目】

给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。

示例:

输入:
words = [“oath”,“pea”,“eat”,“rain”] and board =
[
[‘o’,‘a’,‘a’,‘n’],
[‘e’,‘t’,‘a’,‘e’],
[‘i’,‘h’,‘k’,‘r’],
[‘i’,‘f’,‘l’,‘v’]
]

输出: [“eat”,“oath”]

【思路】
在|版本上增加代码代码,如下:

vector<string> findWords(vector<vector<char>>& board, vector<string>& words)
    {
        vector<string>res;
        for(int i=0;i<words.size();i++)
        {
            if(exist(board,words[i]))
            res.insert(res.begin(),words[i]);

        }
        return res;
    }

结果正确但是超时。
在这里插入图片描述在这里插入图片描述
需要解决问题:
在这里插入图片描述【改进】
前缀树,代码摘自一个大佬,做了相关注释

【注释】
123

【代码】

class TrieNode{
public:
    string word = "";
    vector<TrieNode*> nodes;
    TrieNode():nodes(26, 0){}
};

class Solution {
    int rows, cols;
    vector<string> res;
public:
    vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
        rows = board.size();
        cols = rows ? board[0].size():0;
        if(rows==0 || cols==0) return res;

        //建立字典树的模板
        TrieNode* root = new TrieNode();
        for(string word:words){
            TrieNode *cur = root;
            for(int i=0; i<word.size(); ++i){
                int idx = word[i]-'a';
                if(cur->nodes[idx]==0) cur->nodes[idx] = new TrieNode();
                cur = cur->nodes[idx];
            }
            cur->word = word;
        }

        //DFS模板
        for(int i=0; i<rows; ++i){
            for(int j=0; j<cols; ++j){
                dfs(board, root, i, j);
            }
        }
        return res;
    }

    void dfs(vector<vector<char>>& board, TrieNode* root, int x, int y){
        char c = board[x][y];
        //递归边界
        if(c=='.' || root->nodes[c-'a']==0) return;
        root = root->nodes[c-'a'];
        if(root->word!=""){
            res.insert(res.begin(),root->word);
            root->word = "";
        }
        
        board[x][y] = '.';
        if(x>0) dfs(board, root, x-1, y);
        if(y>0) dfs(board, root, x, y-1);
        if(x+1<rows) dfs(board, root, x+1, y);
        if(y+1<cols) dfs(board, root, x, y+1);
        board[x][y] = c;
    }  
};



【结果】
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值