79力扣:单词搜索

cpp 复制代码
class Solution {
public:
    bool exist(vector<vector<char>>& board, string word) {
        int key=0;
        int row=board.size();
        int col=board[0].size();
        int wSize=word.size();
        function<void(int,int,int)> dfs=[&](int m,int n,int index){
            if(key==1){return ;}
            if(board[m][n]==word[index]){
                char tmp=board[m][n];
                board[m][n]='*';
                if(index==(word.size()-1)){
                    key=1;
                }else{
                if(m-1>=0){dfs(m-1,n,index+1);}
                if(m+1<=row-1){dfs(m+1,n,index+1);}
                if(n-1>=0){dfs(m,n-1,index+1);}
                if(n+1<=col-1){dfs(m,n+1,index+1);}
                }
                board[m][n]=tmp;
            }
        };
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(board[i][j]==word[0]){
                    dfs(i,j,0);
                    if(key==1) return true;
                }
            }
        }
        return false;
    }
};

对于字符串中的某个字符直接用wordindex表示。

在 C++ 中,std::to_string 函数用于将各种基本类型转换为对应的 std::string 类型。其具体行为取决于参数的类型,如下所示:

  • 整数类型int, long, long long, unsigned int, 等等)将被转换为其十进制表示的字符串。
  • 浮点数类型float, double, long double)将被转换为其小数形式的字符串,通常包括小数点和指数部分。
  • 字符类型char, wchar_t, char16_t, char32_t)将被转换为长度为 1 的字符串,包含字符本身。
  • 布尔类型bool)将被转换为 "true""false" 的字符串。
相关推荐
happymaker062624 分钟前
LeetCodeHot100——155.最小栈
算法
洛水水35 分钟前
【力扣100题】85.每日温度
算法·leetcode·职场和发展
Coder-magician39 分钟前
《代码随想录》刷题打卡day15:二叉树part05
数据结构·c++·算法
Kurisu_红莉栖39 分钟前
力扣56合并区间
算法·leetcode
Irissgwe1 小时前
算法的时间复杂度和空间复杂度
数据结构·c++·算法·c·时间复杂度·空间复杂度
随意起个昵称1 小时前
区间dp-基础题目3(永别)
c++·算法
周末也要写八哥1 小时前
有向图Hierholzer算法的另一种实现
算法
bIo7lyA8v1 小时前
算法调优中的性能回归与基准测试分析的技术8
算法·数据挖掘·回归
有点。1 小时前
C++贪心算法二(练习题)
c++·算法·贪心算法
西安邮电大学1 小时前
贪心算法详细讲解
java·后端·其他·算法·面试