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" 的字符串。
相关推荐
qq_1927798717 分钟前
C++模块化编程指南
开发语言·c++·算法
cici158742 小时前
大规模MIMO系统中Alamouti预编码的QPSK复用性能MATLAB仿真
算法·matlab·预编码算法
历程里程碑2 小时前
滑动窗口---- 无重复字符的最长子串
java·数据结构·c++·python·算法·leetcode·django
2501_940315263 小时前
航电oj:首字母变大写
开发语言·c++·算法
CodeByV4 小时前
【算法题】多源BFS
算法
TracyCoder1234 小时前
LeetCode Hot100(18/100)——160. 相交链表
算法·leetcode
浒畔居4 小时前
泛型编程与STL设计思想
开发语言·c++·算法
独处东汉4 小时前
freertos开发空气检测仪之输入子系统结构体设计
数据结构·人工智能·stm32·单片机·嵌入式硬件·算法
乐迪信息5 小时前
乐迪信息:AI防爆摄像机在船舶监控的应用
大数据·网络·人工智能·算法·无人机
放荡不羁的野指针5 小时前
leetcode150题-滑动窗口
数据结构·算法·leetcode