当前位置: 首页 > news >正文

【每日刷题】Day155

【每日刷题】Day155

🥕个人主页:开敲🍉

🔥所属专栏:每日刷题🍍

🌼文章目录🌼

1. LCR 108. 单词接龙 - 力扣(LeetCode)

2. 675. 为高尔夫球比赛砍树 - 力扣(LeetCode)

3. LCR 107. 01矩阵 - 力扣(LeetCode)

1. LCR 108. 单词接龙 - 力扣(LeetCode)

//思路:本题与 Day154 中的 "最小基因变化" 完全一样

class Solution {

public:

    string total = "abcdefghijklmnopqrstuvwxyz";

    int ladderLength(string beginWord, string endWord, vector<string>& wordList)

    {

        unordered_set<string> hash(wordList.begin(),wordList.end());//标记字典中的单词

        unordered_set<string> se;//标记已经出现过的单词

        if(!hash.count(endWord)||beginWord==endWord) return 0;

        queue<string> qu;

        qu.push(beginWord);

        se.insert(beginWord);

       

        int ans = 0;

        while(!qu.empty())

        {

            ans++;

            int size = qu.size();

            for(int i = 0;i<size;i++)

            {

                string s = qu.front();

                for(int j = 0;j<s.size();j++)

                {

                    for(int m = 0;m<26;m++)

                    {

                        string tmp = s;

                        if(tmp[j]==total[m]) continue;

                        tmp[j] = total[m];

                        if(tmp==endWord) return ans+1;

                        if(se.count(tmp)) continue;

                        se.insert(tmp);

                        if(hash.count(tmp)) qu.push(tmp);

                    }

                }

                qu.pop();

            }

        }

        return 0;

    }

};

2. 675. 为高尔夫球比赛砍树 - 力扣(LeetCode)

//思路:BFS+排序 

//本题可以看作是 Day154 中 "迷宫中离入口最近的出口" 的升级版。

//仔细思考一下,本题要求我们从 (0,0)位置开始砍树,必须按照 由低到高 的顺序进行砍树,那我们首先第一步一定是先把数组中的所有树的高度按照升序排序,并且排序后我们还要保留其原本所在的坐标。

//排好序后,我们要从最低的树开始砍,砍完当前树需要去到下一个更高的树,因此这里我们实际上可以将砍树的动作看作为:从低的树的坐标,去到下一个更高的树的坐标;再从下一个更高的树的坐标,去到下下一个更更高的树的坐标 ...... ,以此类推。

//到这,本题实际上就是多次 从一个坐标去到另一个坐标 的操作,与  "迷宫中离入口最近的出口" 这题的思路就完全一样了。

class Solution {

public:

    int n, m;

    int dx[4] = { 1,-1,0,0 };

    int dy[4] = { 0,0,1,-1 };

    int cutOffTree(vector<vector<int>>& forest)

    {

        n = forest.size(), m = forest[0].size();

        int ans = 0;

        vector<vector<int>> hash;//这里也可以用 pair,如果用pair后面 sort排序的时候就要自己写 lambda 表达式来排序,这里偷懒的方法就是用二维数组。

        for (int i = 0; i < n; i++)

            for (int j = 0; j < m; j++)

                if (forest[i][j] > 1)

                      hash.push_back({forest[i][j],i,j});//0位置放值,也就是树的高度,1、2位置放坐标

        sort(hash.begin(), hash.end());//排序时默认会按照数组第一个元素进行排序,这里刚好就是根据树的高度进行排序

        int bx = 0, by = 0;//从 (0,0)位置开始

        for (int i = 0; i < hash.size(); i++)

        {

            auto a = hash[i];

            int ex = a[1], ey = a[2];//拿到下一个位置的坐标

            int ret = bfs(forest, bx, by, ex, ey);//获得从 bx、by(初始位置)到达ex、ey(目标位置)所需的最少步数

            if (ret == -1) return -1;//没法到达说明一定有至少一棵树没法砍掉,返回-1

            ans += ret;

            bx = ex, by = ey;//更新初始位置

        }

        return ans;

    }

//下面的代码与 "迷宫中离入口最近的出口" 完全一样

    int bfs(vector<vector<int>>& forest, int bx, int by, int ex, int ey)

    {

        if(bx==ex&&by==ey) return 0;

        vector<vector<bool>> hash(n, vector<bool>(m));

        queue<pair<int, int>> qu;

        qu.push({bx,by});

        hash[bx][by] = true;

        int ret = 0;

        while (!qu.empty())

        {

            ret++;

            int size = qu.size();

            while(size--)

            {

                auto a = qu.front();

                qu.pop();

                for (int j = 0; j < 4; j++)

                {

                    int x = a.first + dx[j], y = a.second + dy[j];

                    if (x >= 0 && x < n && y >= 0 && y < m && !hash[x][y] && forest[x][y])

                    {

                        if (x == ex && y == ey) return ret;

                        qu.push({ x,y });

                        hash[x][y] = true;

                    }

                }

            }

        }

        return -1;

    }

};

3. LCR 107. 01矩阵 - 力扣(LeetCode)

//思路:多源BFS

//多源BFS问题相较于单源BFS问题而言,区别仅仅在于:单源BFS初始只将一个起点放入队列向外扩展;多源BFS则是将所有起点同时放入队列进行向外扩展。

//本题我们要从逆向思维来考虑问题:题目让我们找到矩阵中所有位置的 1 到它最近的 0 的距离,并将距离与 1 替换。

//反向思考:我们将所有的 0 看作一个起点向外扩展,每次扩展到 1 时直接记录当前距离,一定是最短距离(因为每个 1 都一定有一个距离最近的 0 ,将所有 0 视为同一个起点同时向外扩展时,距离 某个 1 的某个 0一定先扩展到,因此每次的距离一定是最短的)

class Solution {

public:

    int n,m;

    int dx[4] = {1,-1,0,0};

    int dy[4] = {0,0,1,-1};

    vector<vector<int>> updateMatrix(vector<vector<int>>& mat)

    {

        n = mat.size(),m = mat[0].size();

        vector<vector<bool>> hash(n,vector<bool>(m));

        queue<vector<int>> qu;

        for(int i = 0;i<n;i++)

            for(int j = 0;j<m;j++)

                if(!mat[i][j])

                {

                    qu.push({i,j});//将所有 0 放入队列,视为一个起点,同时向外扩展

                    hash[i][j] = true;

                }

       

        int ret = 0;

        while(!qu.empty())

        {

            ret++;

            int size = qu.size();

            while(size--)

            {

                auto arr = qu.front();

                qu.pop();

                for(int j = 0;j<4;j++)

                {

                    int x = arr[0]+dx[j],y = arr[1]+dy[j];

                    if(x>=0&&x<n&&y>=0&&y<m&&!hash[x][y])

                    {

                        if(mat[x][y]) mat[x][y] = ret;//扩展到 1 时,此时的距离一定是最短的(因为此时一定是距离最近的 0 先扩展到 1)

                        qu.push({x,y});

                        hash[x][y] = true;

                    }

                }

            }

        }

        return mat;

    }

};

http://www.lryc.cn/news/484504.html

相关文章:

  • EXCEL延迟退休公式
  • 开源对象存储新选择:在Docker上部署MinIO并实现远程管理
  • Spring Cloud生态圈
  • AI视觉小车基础--4.舵机控制(云台控制)
  • 【Rust中的项目管理】
  • 【原创】如何备份和还原Ubuntu系统,非常详细!!
  • 成都栩熙酷网络科技抖音小店是真的
  • Python 爬虫数据清洗与存储:基础教程
  • ssm122基于Java的高校教学业绩信息管理系统+jsp(论文+源码)_kaic
  • Java 基础知识
  • 深入探索 React Hooks:原理、用法与性能优化全解
  • python中父类和子类继承学习
  • Linux——GPIO输入输出裸机实验
  • 华为鸿蒙HarmonyOS NEXT升级HiCar:打造未来出行新体验
  • 【项目组件】第三方库——websocketpp
  • 计算机23级数据结构上机实验(第3-4周)
  • 【大数据学习 | HBASE高级】region split机制和策略
  • flink实战 -- flink SQL 实现列转行
  • React中右击出现自定弹窗
  • Unity类银河战士恶魔城学习总结(P128 Switch UI with KeyBoard用键盘切换UI)
  • 基于Springboot+微信小程序的急救常识学习系统 (含源码数据库)
  • 【云计算解决方案面试整理】3-7主流云计算平台、云计算架构、安全防护
  • 数据库范式、MySQL 架构、算法与树的深入解析
  • 设计模式之责任链模式(Chain Of Responsibility)
  • SQLite 全文检索:快速高效的文本查询方案
  • 【微信小程序】报修管理
  • C++——视频问题总结
  • Ubuntu24.04 network:0 unclaimed wireless adapter no found
  • Java 使用MyBatis-Plus数据操作关键字冲突报错You have an error in your SQL syntax问题
  • 深入浅出 ChatGPT 底层原理:Transformer