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;
    }
};

对于字符串中的某个字符直接用word[index]表示。

在 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" 的字符串。
相关推荐
小雅痞14 分钟前
[Java][Leetcode hard] 68. 文本左右对齐
java·开发语言·leetcode
6Hzlia17 分钟前
【Hot 100 刷题计划】 LeetCode 102. 二叉树的层序遍历 | C++ 极简 DFS 巧解
c++·leetcode·深度优先
人道领域38 分钟前
【LeetCode刷题日记】225.用队列实现栈--三招实现栈操作(多种思维)
java·开发语言·算法·leetcode·面试
新新学长搞科研1 小时前
【高届数机械工程会议】第十二届机械工程、材料和自动化技术国际学术会议(MMEAT 2026)
运维·人工智能·算法·机器学习·自动化·软件工程·激光
狐璃同学1 小时前
数据结构(2)线性表
数据结构·算法
啦啦啦_99991 小时前
4. KNN算法之 特征预处理(归一化&标准化)
算法
淘气包海鸟1 小时前
雷达基本原理
算法·信息与通信
Tisfy1 小时前
LeetCode 2615.等值距离和:分组(哈希表+前缀和)
算法·leetcode·散列表
啦啦啦_99992 小时前
2. KNN算法之 分类&回归API实现
算法
X journey2 小时前
机器学习进阶(23):K-means聚类
人工智能·算法·机器学习